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

Animation won't play when touched. Ideas?

Beginner here. Prepare to see the messiest code you've seen. Attempting to make a puddle. No errors appear in the output or console but according to the prints in the code the touch function doesn't want to cooperate. puddleAb is the hitbox of the puddle.

print('line 1')
repeat wait() until game.Players.LocalPlayer and game.Players.LocalPlayer.Character
local Player = game.Players.LocalPlayer
local Character = Player.Character
local puddle = game.Workspace.puddlething.puddleAb
puddle.Touched:Connect(function()
print('touched') --this print doesn't appear in output
 local Anim = Instance.new('Animation',Character) 
 Anim.AnimationId = 'rbxassetid://3272402216' 
local yo = Character.Humanoid:LoadAnimation(Anim)
yo:Play()
wait(2) --wait time the same as thje animation time hhhhhhh
yo:Stop()
end)

If you can understand what an abomination I have created, help me out here? Please?

2 comments

0

Use localscript Because animations are local

0

Dm me, Freddan2006ENG#5537, and it's not fe so it will only be visible to you btw.

1 answer

✓ Accepted answer

Your code is quite messy, but since you ask nicely.. OK. First things first. You can either play animations on a server or the client, since they replicate both ways. It is however encouraged to play animations on the client, so we will do just that. Your script should be a LocalScript, can be named whatever you like, and should be placed in the StarterPlayer -> StarterCharacterScripts. IMPORTATNT: Please manually create a new RemoteEvent in the ReplicatedStorage and change its name to "PlayYoAnimation". Regarding your local script - You need to make sure your animation is loaded just once and then played whenever necessary. Here is your StarterCharacterScript:

local character = script.Parent
local Anim = Instance.new("Animation")  --do not parent this to workspace
Anim.AnimationId = "http://www.roblox.com/rbxassetid://3272402216"
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PlayYoAnimation = ReplicatedStorage:WaitForChild("PlayYoAnimation")
local yo = character.Humanoid:LoadAnimation(Anim)

local animationPlaying = false
function OnPlayYoAnimation()
	if not animationPlaying then
		animationPlaying = true
		yo:Play()
		wait(2) --wait time the same as thje animation time hhhhhhh
		yo:Stop()
		animationPlaying = false
	end
end)
PlayYoAnimation.OnClientEvent:Connect(OnPlayYoAnimation)

We still need to write a server script. You have decided to use a Touched event. There are few thing you need to take care of when using these. I will guide you through them. Your Script (regular server script) should be Parented to the puddleAb part. Why? Because this is the easiest way to create as many puddles as you like without too much complication. Here goes: First write your touched function:

local puddleAb = script.Parent

function OnTouched(hit)
end
puddleAb.Touched:Connect(OnTouched)

Connecting function later is just preference, but it looks more neatly in my opinion. Next step is to add a debounce, since Touched events fire multiple times for humanoids. I prefer to name debounce "justOnce", as it makes more sense to me:

local puddleAb = script.Parent

local justOnce = true 
function OnTouched(hit)
	if justOnce then 
		justOnce = false
		wait(1)
		justOnce = true
	end
end
puddleAb.Touched:Connect(OnTouched)

Now that we have a debounce, we need to make sure we ignore events triggered by non-humanoids:


local puddleAb = script.Parent

local justOnce = true 
function OnTouched(hit)
	if justOnce and hit.Parent:FindFirstChild("HumanoidRootPart") then 
		justOnce = false
		wait(1)
		justOnce = true
	end
end
puddleAb.Touched:Connect(OnTouched)

Finally we need to fire an event to a client, to inform him/her to play an animation. I hope you have created RemoteEvent as I told you already, otherwise it will not work:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PlayYoAnimation = ReplicatedStorage.PlayYoAnimation -- no need to use WaitForChild on server

local puddleAb = script.Parent

local justOnce = true 
function OnTouched(hit)
	if justOnce and hit.Parent:FindFirstChild("HumanoidRootPart") then 
		justOnce = false
		PlayYoAnimation:FireClient() --will not work
		wait(1)
		justOnce = true
	end
end
puddleAb.Touched:Connect(OnTouched)

But wait.. How do we know which player to inform. Is player the same as character. No of course it is not. But we can use a little trick to find a player

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PlayYoAnimation = ReplicatedStorage.PlayYoAnimation -- no need to use WaitForChild on server

local puddleAb = script.Parent

local justOnce = true 
function OnTouched(hit)
	if justOnce and hit.Parent:FindFirstChild("HumanoidRootPart") then 
		justOnce = false
		local player = Players:GetPlayerFromCharacter(hit.Parent)
		PlayYoAnimation:FireClient(player)
		wait(1)
		justOnce = true
	end
end
puddleAb.Touched:Connect(OnTouched)

I hope it will work for you. Have a nice coding weekend :)

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