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

Why do these animations not work in-game?

local player = game.Players.LocalPlayer
repeat wait() until player.Character.Humanoid
local humanoid = player.Character.Humanoid
local mouse = player:GetMouse()
local saluteButton = game.Players.LocalPlayer:WaitForChild("PlayerGui").MobileEmotes.Salute

local anim = Instance.new("Animation")
anim.AnimationId = "http://www.roblox.com/asset/?id=5191369819"




local function onClick()
	local playAnim = humanoid:LoadAnimation(anim)
		 playAnim:Play()
end

saluteButton.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

✓ Accepted answer

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.

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

Wich is better than using repeat wait() until character

So, implementing this to your code.

local players = game:GetService("Players")
local player = players.LocalPlayer
local character = player.CharacterAdded:Wait() or player.Character
local humanoid = character:WaitForChild("Humanoid")
local mouse = player:GetMouse()
local saluteButton = player:WaitForChild("PlayerGui").MobileEmotes.Salute

local anim = Instance.new("Animation")
anim.AnimationId = "http://www.roblox.com/asset/?id=5191369819"


local function onClick()
    local playAnim = humanoid:LoadAnimation(anim)
         playAnim:Play()
end

saluteButton.MouseButton1Click:Connect(onClick)

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

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