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

How to make an animation on Mob/NPC on the touch of a block by a local player happen?

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!!!

1 answer

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.

3 comments

0

I'm afraid it didn't work... have any other ideas?

0

Either its the animation script or you need to make a new method...?I don't have much time to help scripting a proper animation scrip , sorry.

0

thats fine, thanks for the help!

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