✓ Accepted answer
To make it so that only a humanoid can make the function play you need to insert a check before the function fires off the animation
i.e.
script.Parent.Touched:connect(function (hit)
if hit.Parent and hit.Parent:FindFirstChild("Humanoid")
then game.Players.LocalPlayer:Play(xw)
else nil
end
Here's a code to play animation when a player touches the electric fence, you can use it if you change the variables to match your game
local animation = Instance.new("Animation")
animation.AnimationId = "http://www.roblox.com/Asset?ID=144911345"
local animTrack = nil
local canPlay = true
function playShockAnim(Source)
if canPlay then
local player = game.Players.LocalPlayer.Character
canPlay = false
animTrack = player.Humanoid:LoadAnimation(animation)
animTrack.KeyframeReached:connect(function(keyframeName)
if keyframeName == "ElectrocuteEnd" then
canPlay = true
end
end)
animTrack:Play()
end
end
1 comment
Thank you, I'll look forward to edit this for my game.