Here are my scripts:
01 | wait() |
02 | local CanEmote = true |
03 | local Player = game.Players.LocalPlayer |
04 | local character = Player.Character or Player.CharacterAdded:Wait() |
05 | local Humanoid = character.Humanoid |
06 | local Animation = Humanoid:LoadAnimation(script.Animation) |
07 |
08 | script.Parent.MouseButton 1 Click:Connect( function () |
09 | if CanEmote then |
10 | Animation:Play() |
11 | CanEmote = false |
12 | wait( 1 ) |
13 | CanEmote = true |
14 | end |
15 | end ) |
Can you help?
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.01 | wait() |
02 | local Player = game.Players.LocalPlayer |
03 | local character = Player.Character or Player.CharacterAdded:Wait() -- waiting for character |
04 | local Humanoid = character.Humanoid |
05 | local Animation 1 = Humanoid:LoadAnimation(script.Animation 1 ) |
06 | local Animation 2 = Humanoid:LoadAnimation(script.Animation 2 ) |
07 | local Animation 3 = Humanoid:LoadAnimation(script.Animation 3 ) |
08 | local CanEmote = true |
09 | script.Parent.MouseButton 1 Click:Connect( function () |
10 | local emote = math.random( 1 , 3 ) |
11 | if emote = = 1 and CanEmote then -- == true unnecessary |
12 | Animation 1 :Play() |
13 | CanEmote = false |
14 | wait( 2 ) |
15 | CanEmote = true |
local
variables, as you didn't need to use them globally as they were defined at the top of the script.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...
1 | Humanoid = game.Players.LocalPlayer.Character:FindFirstChild( "Humanoid" ) |
You should use...
1 | 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...
1 | --Line 09, 16 and 22 btw |
2 | Animation 1 :Play() |
You should use...
1 | --Wherever you put the BindableEvent, for my example case I put it in ServerStorage |
2 | game:GetService( "ServerStorage" ).BindableEvent:Fire(Animation 1 ) --Sending an argument off |
And in another server-script you can receive the firing of the event and play the animation e.g.
1 | game:GetService( "ServerStorage" ).BindableEvent.Event:connect( function (arg 1 ) -- the arg we sent earlier |
2 | arg 1 :Play() |
3 | end ) |
I hope my answer helped or contributed to you. ~tantec