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")
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)
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
local puddleAb = script.Parent
local justOnce = true
function OnTouched(hit)
if justOnce and hit.Parent:FindFirstChild("HumanoidRootPart") then
justOnce = false
PlayYoAnimation:FireClient()
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
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 :)
2 comments
Use localscript Because animations are local
Dm me, Freddan2006ENG#5537, and it's not fe so it will only be visible to you btw.