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

Putting Animations in Game Using a Touch Event?

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

✓ Accepted answer

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.

3 comments

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.

0

I shall edit it for you now so it'll work with NPCs.

0

All edited, should now work with NPCs too.

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