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

Putting Animations in Game Using a Touch Event?

Asked by 4 years ago

I've made all the animations I need for my game. I just have to put a script in the block that I need to touch, then it will play the animation. I never figured that out. I know that this isn't a request site, but this is important if I want to finish my game. Do any of you know how this is done.

1 answer

Log in to vote
0
Answered by
pwx 1581 Moderation Voter
4 years ago
Edited 4 years ago

Well, this is actually relatively simple to do.

All you'd need to do is:

local Part = workspace.Part -- define the part
local Animation = Part.Animation -- define animation

local Players = game:GetService('Players') -- Services


function onTouched(otherPart)

    local Humanoid = otherPart.Parent:FindFirstChildOfClass('Humanoid') -- you'll need this
    if Humanoid then
        local Humanoid = otherPart.Parent.Humanoid

        for _,track in pairs(Humanoid:GetPlayingAnimationTracks()) do
            if track.Animation.Name ~= Animation.Name then -- make sure isnt already playing     
            local newTrack = Humanoid:LoadAnimation(Animation)
            newTrack:Play()
            end -- check animation name end
        end -- loop end
    end -- check humanoid
end -- function end


Part.Touched:Connect(onTouched) -- connect to function

I probably made it a lot more complicated than I need to but I'm hoping the GetPlayingAnimationTracks will act as a debounce.

0
Will this work with NPC's as well. What I mean is when you touch the part, the animation for the NPC is playing. Captain0Jack0Sparrow 26 — 4y
0
I shall edit it for you now so it'll work with NPCs. pwx 1581 — 4y
0
All edited, should now work with NPCs too. pwx 1581 — 4y
Ad

Answer this question