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

Why do these animations not work in-game?

Asked by 4 years ago
Edited 4 years ago
01local player = game.Players.LocalPlayer
02repeat wait() until player.Character.Humanoid
03local humanoid = player.Character.Humanoid
04local mouse = player:GetMouse()
05local saluteButton = game.Players.LocalPlayer:WaitForChild("PlayerGui").MobileEmotes.Salute
06 
07local anim = Instance.new("Animation")
09 
10 
11 
12 
13local function onClick()
14    local playAnim = humanoid:LoadAnimation(anim)
15         playAnim:Play()
16end
17 
18saluteButton.MouseButton1Click:Connect(onClick)

yes, this script works in Roblox Studio, but when I go onto an actual game server it does not work, can someone please help me out?

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

The problem here is that you are indexing character with humanoid, and sometimes it doesn't exist and may error.

To fix this you could wait for the character and humanoid, maybe doing this.

1local character = players.LocalPlayer.CharacterAdded:Wait()

Wich is better than using repeat wait() until character

So, implementing this to your code.

01local players = game:GetService("Players")
02local player = players.LocalPlayer
03local character = player.CharacterAdded:Wait() or player.Character
04local humanoid = character:WaitForChild("Humanoid")
05local mouse = player:GetMouse()
06local saluteButton = player:WaitForChild("PlayerGui").MobileEmotes.Salute
07 
08local anim = Instance.new("Animation")
10 
11 
12local function onClick()
13    local playAnim = humanoid:LoadAnimation(anim)
14         playAnim:Play()
15end
16 
17saluteButton.MouseButton1Click:Connect(onClick)

If this answered your question then mark it as the answer, it would be highly appreciated!

Ad

Answer this question