Jump to content

Enemy says something on hit


Recommended Posts

Hello! I need to make it so when my character hits something, they will say something.

I don't want to make it so that when any character hits something, it says "example", Only this one character

 

 

Link to comment
Share on other sites

use in your postinit from your character
inst:ListenForEvent("onattackother", OnAttackOther)

Then you define your OnAttackOther(inst,data). data contains data.target So you can check if your target is a player and make him say sth.

Edited by Serpens
Link to comment
Share on other sites

44 minutes ago, Augustusc said:

Will that work if i hit other creatures that speak (like a pigman for example)?

data.target is the thing you hit, it can be an animal, a structure or a player.
You have to check yourself what it is and if it has the "talker" component. If it has (I think pigs have it) then you can make it say something.

Link to comment
Share on other sites

Put this in your character's OnLoad function:

inst:ListenForEvent("onattackother", OnHitEnemy)

Then add this function:

local function OnHitEnemy(inst, data)
	local saythese = {
		"Put words here",
		"And here",
		"Maybe here too",
	}
	
	local targ = data.target or nil
	if inst ~= nil and targ ~= nil then
		if targ.components.talker == nil then
			targ:AddComponent("talker")
		end
		targ.components.talker:Say( saythese[ math.random( 1, table.getn(saythese) ) ] )
		if targ.components.talker.offset == nil then
			targ.components.talker.offset = Vector3(0,
			((targ:HasTag("smallcreature") or targ:HasTag("small")) and -300)
			or (targ.components.explosiveresist and -900)
			or ((targ:HasTag("largecreature") or targ:HasTag("epic") or targ:HasTag("large")) and -700)
			or (-400), 0)
		end
	end
end

 

Edited by JohnWatson
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
  • Create New...