How to transfer a variable

So I have a function that notices the enemy I’m attacking, and I need to take out of the loop and load the animation from this humanoid only problem is that I outside the function touched can not use humanoid. how do I transfer the variable?

	local function OnTouched(otherPart)	
			if otherPart.Name == "Head" or "Torso" and otherPart.Parent:FindFirstChild("Humanoid") ~= nil and inCombat.Value == true then
				local currentTarget = otherPart.Parent:FindFirstChild("Humanoid")
				--print(currentTarget)
			else
				return
			end
		end
		
		humanoid.Touched:Connect(OnTouched)
		
		local load = humanoid:LoadAnimation(animation)
		load:Play()
		
		local loadHit = currentTarget:LoadAnimation(hitAnimation) -- script can't find currentTarget
		loadHit:Play()
1 Like

You can do this:

local currentTarget: Humanoid = nil

local function OnTouched(otherPart)
	if otherPart.Name == "Head" or "Torso" and otherPart.Parent:FindFirstChild("Humanoid") ~= nil and inCombat.Value == true then
		currentTarget = otherPart.Parent:FindFirstChild("Humanoid")
		--print(currentTarget)
	else
		return
	end
end
		
humanoid.Touched:Connect(OnTouched)
		
local load = humanoid:LoadAnimation(animation)
load:Play()


local loadHit = currentTarget:LoadAnimation(hitAnimation) -- script can't find currentTarget
loadHit:Play()

You need to change how loadHit is used though, as right now it will do loadHit instantly after playing load, but there is no guarantee that there will be a currentTarget at that stage.

1 Like

What if I move loadHit to touched? That way when the player hits the enemy the animation will play

1 Like

Yes, that makes the most sense. That way, you won’t need to transfer the variable either, and you may just do this:

local function OnTouched(otherPart)
	if otherPart.Name == "Head" or "Torso" and otherPart.Parent:FindFirstChild("Humanoid") ~= nil and inCombat.Value == true then
		local currentTarget = otherPart.Parent:FindFirstChild("Humanoid")
		local loadHit = currentTarget:LoadAnimation(hitAnimation) -- script can't find currentTarget
		loadHit:Play()
	else
		return
	end
end
		
humanoid.Touched:Connect(OnTouched)
		
local load = humanoid:LoadAnimation(animation)
load:Play()

Okay, but I was thinking that it probably makes more sense to do it like this:

tool.Touched:Connect(OnTouched)

Otherwise, I’ll just use this feature most of the time.

But the problem is that there is such a bug… What do you think we should do?

I think you have to use the handle part for Touched.
So it’s

tool.Handle.Touched:Connect(OnTouched)

So here I have two problems. The first one is this:


Here’s the script:

local function OnTouched(otherPart)
			if otherPart.Name == "Head" or "Torso" and otherPart.Parent:FindFirstChild("Humanoid") ~= nil and inCombat.Value == true then
				wait(1)
				local currentTarget = otherPart.Parent:FindFirstChild("Humanoid")
				local loadHit = currentTarget:LoadAnimation(hitAnimation)
				loadHit:Play()
			else
				return
			end
		end

and the second is that after a while the impact begins to sluggish as I understand it is because I try a bunch of times to play the animation