This is located in a server script. For most scripts I would be able to load the animation before hand, but how would I do that with a touch event? Since getting the humanoid has to be within the event wouldn’t I have to load the animationTrack within it as well?
wire.Touched:Connect(function(hit)
local partParent = hit.Parent
local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
if humanoid then
local targetChar = humanoid.Parent
if hit.Parent.Name ~= "Dummy" and not table.find(hitList,targetChar) then
local zapTrack = game.ReplicatedStorage.PlayerAnimations.Zapped
local zapAnimation = humanoid:WaitForChild("Animator"):LoadAnimation(zapTrack)
you can disconnect the event when the humanoid is detected using :Disconnect
youll need to have a variable for the event to disconnect it
like this
local ZapAnimation
local Connection
Connection = wire.Touched:Connect(function(hit)
local partParent = hit.Parent
local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
if humanoid then
local targetChar = humanoid.Parent
if hit.Parent.Name ~= "Dummy" and not table.find(hitList,targetChar) then
Connection:Disconnect()
local zapTrack = game.ReplicatedStorage.PlayerAnimations.Zapped
ZapAnimation = humanoid:WaitForChild("Animator"):LoadAnimation(zapTrack)
end
end
end)
--// Example to get the zap anim thingy
ZapAnimation:Play()
then youll need to store each loaded animations
animations should be loaded once
try
local StoredzapTracks = {}
wire.Touched:Connect(function(hit)
local partParent = hit.Parent
local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
if humanoid then
local targetChar = humanoid.Parent
if hit.Parent.Name ~= "Dummy" and not table.find(hitList,targetChar) then
local zapAnimation = game.ReplicatedStorage.PlayerAnimations.Zapped
if StoredzapTracks[targetChar] then
StoredzapTracks[targetChar][zapAnimation]:Play()
else
StoredzapTracks[targetChar] = humanoid:LoadAnimation(zapAnimation)
StoredzapTracks[targetChar][zapAnimation]:Play()
end
end
end
end)
and btw why wont you just make a script each on the characters so you can just load the animation once easily without loading the animation inside the event