Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to make the local player play an animation when touches a brick?

If anyone could help me fix this script, I would appreciate it a lot:

xw = script.Parent.PushDoorAnimation

function PushDoor()
	wait(0.1)
	game.Players.LocalPlayer:Play(xw)
end
script.Parent.Touched:connect(function PushDoor()
end)

Also, how can you make it so only a humanoid can make the function play?

1 answer

✓ 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

-- Import animation
local animation = Instance.new("Animation")
animation.AnimationId = "http://www.roblox.com/Asset?ID=144911345"
 
-- Local variables
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) -- Load animation into Humanoid
		animTrack.KeyframeReached:connect(function(keyframeName) -- Bind function to KeyframeReached event
			if keyframeName == "ElectrocuteEnd" then
				canPlay = true
			end
		end)	
		animTrack:Play() -- Start the animation
	end
end

1 comment

0

Thank you, I'll look forward to edit this for my game.

This discussion is preserved as it was. New answers, comments, and votes are closed.