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

My animation isn't playing in actual Roblox, but working in Roblox studio?

Asked by 5 years ago
Edited 5 years ago

I have a working animation that plays in Roblox studio, but for some strange reason it won't play in a real server in Roblox.

Here are my scripts:

wait()
local CanEmote = true
local Player = game.Players.LocalPlayer
local character = Player.Character or Player.CharacterAdded:Wait() 
local Humanoid = character.Humanoid
local Animation = Humanoid:LoadAnimation(script.Animation)

script.Parent.MouseButton1Click:Connect(function()
    if CanEmote then
    Animation:Play()
    CanEmote = false
    wait(1)
    CanEmote = true
    end
end)


Can you help?

0
I will try the scripts out RetroGalacticGamer 331 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago

Animations played from a LocalScript replicate to the server, as they are a part of player physics, just like how Humanoid changes replicate to the server. You're getting that error because the character isn't loaded when the player joins the game, you'll have to wait for the character to load.

wait()
local Player = game.Players.LocalPlayer
local character = Player.Character or Player.CharacterAdded:Wait() -- waiting for character
local Humanoid = character.Humanoid
local Animation1 = Humanoid:LoadAnimation(script.Animation1)
local Animation2 = Humanoid:LoadAnimation(script.Animation2)
local Animation3 = Humanoid:LoadAnimation(script.Animation3)
local CanEmote = true
script.Parent.MouseButton1Click:Connect(function()
    local emote = math.random(1,3)
    if emote == 1 and CanEmote then -- == true unnecessary  
        Animation1:Play()
        CanEmote = false
        wait(2)
        CanEmote = true 
    elseif emote == 2 and CanEmote then
       Animation2:Play()
       CanEmote = false
       wait(2)
       CanEmote = true 

    elseif emote == 3 and CanEmote then
        Animation3:Play()
        CanEmote = false
        wait(2)
        CanEmote = true 
     end                                    
end)

You should also indent your code properly, some lines were properly indented whilst others weren't. You should also use more local variables, as you didn't need to use them globally as they were defined at the top of the script.

0
Thanks so much! now all I have to do is move the script and change the event! I was stuck on this for a while RetroGalacticGamer 331 — 5y
Ad
Log in to vote
0
Answered by
tantec 305 Moderation Voter
5 years ago

You should put this script in StarterPlayer > StarterCharacterScripts. What this will do will allow the script to load along the character ( the scripts load inside the character model in workspace ) and when you do this instead of using...

Humanoid = game.Players.LocalPlayer.Character:FindFirstChild("Humanoid")

You should use...

Humanoid = script.Parent:FindFirstChild("Humanoid")

But you should only use this when you have put the scirpt in the StarterCharacterScripts as it is already in the model which is why I done that.

****Another thing I saw

Also another thing, I was wondering if you wanted the animation to be seeable by other characters seeing as your game was FliteringEnabled, and if you wanted to do this you should use BindableEvents, https://wiki.roblox.com/index.php?title=API:Class/BindableEvent

This allows two scripts to communicate with each other regardless of it being local, server or module and by using this to your advantage, instead of using...

--Line 09, 16 and 22 btw
Animation1:Play()

You should use...

--Wherever you put the BindableEvent, for my example case I put it in ServerStorage
game:GetService("ServerStorage").BindableEvent:Fire(Animation1) --Sending an argument off

And in another server-script you can receive the firing of the event and play the animation e.g.

game:GetService("ServerStorage").BindableEvent.Event:connect(function(arg1) -- the arg we sent earlier
    arg1:Play()
end)

I hope my answer helped or contributed to you. ~tantec

Answer this question