Hello, I have a problem with making an animation happen on an Mob/NPC. I am trying to make an rpg. I want this animation to happen on the touch of a block, but it doesn't seen to work at all. I used the animation editor plugin to make the animation on the npc and all I need is to make that animation happen when the local player touches a specific block.
I put some code in the block and in the NPC but unsurprisingly it didn't work.
Here is the code for the block:
local animation = Instance.new("Animation") animation.AnimationId = "http://www.roblox.com/Asset?ID=863113961" function onTouched() game.Workspace.Destrioggok.Humanoid:GetPlayingAnimationTracks("http://www.roblox.com/Asset?ID=863113961") script:play("http://www.roblox.com/Asset?ID=863113961") end script.Parent.Touched:connect(onTouched)
Here is the code for the NPC:
-- Import Animation local animation = Instance.new("Animation") animation.AnimationId = "http://www.roblox.com/Asset?ID=863113961" -- Local variables local animTrack = nil local canPlay = true function playShockAnim(Source) if canPlay then local playAnimation = game.Destrioggok.Character canPlay = true animTrack = game.Destrioggok.Humanoid:LoadAnimation(animation) -- Load animation into Humanoid animTrack.KeyFrameReached:connect(function(keyframeName) -- Bind function to KeyframeReached event if keyframeName == "end" then canPlay = false end end) animTrack:Play() -- Start the animation end end script.Parent:WaitForChild("ShockEvent").OnClientEvent:connect(playShockAnim) -- Connect remote event to function playing animation
I had no idea what I was doing, but I'm desperate to know how to do it, please help me!!!
Here is the code for the block:
local animation = Instance.new("BoolValue") animation.Value = false animation.Name = "CanPlay" script.Parent.Touched:connect(function(hit) if hit.Parent ~= nil then if hit.Parent:findFirstChild("Humanoid") then if hit.Parent.Character ~= nil then animation.Parent = hit.Parent animation.Value = true end end end end)
For the Humanoid, NPC :
-- Import Animation local animation = Instance.new("Animation") animation.AnimationId = "http://www.roblox.com/Asset?ID=863113961" -- Local variables local animTrack = nil local canPlay = script.Parent:findFirstChild("CanPlay") function playShockAnim(Source) if canPlay then local playAnimation = game.Destrioggok.Character canPlay.Value = false animTrack = game.Destrioggok.Humanoid:LoadAnimation(animation) -- Load animation into Humanoid animTrack.KeyFrameReached:connect(function(keyframeName) -- Bind function to KeyframeReached event if keyframeName == "end" then canPlay.Value = true end end) animTrack:Play() -- Start the animation end end script.Parent:WaitForChild("CanPlay").Changed:connect(playShockAnim)
What I did above is : I created a BoolValue to store the canplay ability. If the NPC touches the part , the BoolValue will transfer to the person or Humanoid that touched it.Then if the Canplay ability is true , the animation plays.
Test it, I am still learning RbxLua.
Thank you for reading.