Hello, I need help with something in my code, I want it to play the normal animation when I press the button for the first time, but if I press the same button again, it stops the animation.
script.Parent.RemoteEvent.OnServerEvent:Connect(function(player)
local character = player.Character or player.CharacterAdded:Wait() -- get the player's character
local humanoid = character:FindFirstChildOfClass("Humanoid") -- get their humanoid
local dance = Instance.new("Animation")
dance.AnimationId = "rbxassetid://5937558680" -- Holiday plays
local animator = humanoid:FindFirstChildOfClass("Animator") or Instance.new("Animator", humanoid) -- find the animator or create a new one
local danc = animator:LoadAnimation(dance)
danc:Play() -- play the animation
end)
Change the LocalScript for this and remove the script and the RemoteEvent.
local Player = game:GetService("Players").LocalPlayer
Character = Player.Character or Player.CharacterAdded:Wait()
Humanoid = Character:WaitForChild("Humanoid")
Animator = Humanoid:WaitForChild("Animator")
local dance = Instance.new("Animation")
dance.AnimationId = "rbxassetid://5937558680"
local danc = Animator:LoadAnimation(dance)
local Play = false
script.Parent.MouseButton1Click:Connect(function()
if Play == false then
danc:Play()
Play = true
else
danc:Stop()
Play = false
end
end)
local player = game:GetService("Players").LocalPlayer
local dance = Instance.new("Animation")
dance.AnimationId = "rbxassetid://5937558680"
local track
function load_animation()
local character = Player.Character
if not character then
return
end
local humanoid = character:FindFirstChild("Humanoid")
if not humanoid then
return
end
track = humanoid:LoadAnimation(dance)
return true
end
script.Parent.MouseButton1Click:Connect(function()
if (not track) and (not load_animation()) then
return
end
if track.IsPlaying then
track:Stop()
else
track:Play()
end
end)