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 3 years ago
Edited 3 years ago
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

Log in to vote
0
Answered by 3 years ago
Edited 3 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.

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!

Ad

Answer this question