Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
Dwayder 29
7 years ago

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

Log in to vote
0
Answered by
Nogalo 148
7 years ago
Edited 7 years ago

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

0
Thank you, I'll look forward to edit this for my game. Dwayder 29 — 7y
Ad

Answer this question