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()
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.
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()
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