i'm not really a good scripter just a basic scripter and i wanted a player to change animation when it hits a certain walkspeed and everyone can see it, heres the script.
01 | local Player = game.Players.LocalPlayer |
02 | local Character = workspace:WaitForChild(Player.Name) |
03 | local Humanoid = Character:WaitForChild( 'Humanoid' ) |
04 |
05 | local RunAnimation = Instance.new( 'Animation' ) |
06 | RunAnimation.AnimationId = 'rbxassetid://3518952951' |
07 | RAnimation = Humanoid:LoadAnimation(RunAnimation) |
08 |
09 | if Humanoid.Walkspeed = = 25 then |
10 | RAnimation:Play() |
11 | elseif Humanoid.Walkspeed = = 4 then |
12 | RunAnimation:Stop() |
13 | end |
can someone help me please?
(it is a script not a localscript, the script is located in serverscriptservice)
The script is a little bit wrong here, and it's in a place where it won't work.
I'd prefer putting this script i'll post down as a normal script or a localscript, in game > StarterPlayer > StarterCharacterScripts
Btw, a localplayer cant be found in a normal script. You can only do localplayer in a localscript.
01 | local Humanoid = script.Parent:FindFirstChild( "Humanoid" ) -- check for humanoid inside player |
02 |
03 | local RunAnimation = Instance.new( 'Animation' ) |
04 | RunAnimation.AnimationId = 'rbxassetid://3518952951' |
05 | RAnimation = Humanoid:LoadAnimation(RunAnimation) |
06 |
07 | if Humanoid.Walkspeed = = 25 then |
08 | RAnimation:Play() |
09 | elseif Humanoid.Walkspeed = = 4 then |
10 | RAnimation:Stop() |
11 | end |
Your script was nearly correct, but it was just in the wrong place. Unless it was getting cloned into players.
Edit: Made a mistake.
Downest have the answer :)
You need to change the StarterPlayer's walk speed so it will work.
Line 9 - 13 will give big problems :(
You should change it to this :
1 | if Humanoid.Walkspeed = = 25 then |
2 | RAnimation:Play() |
3 | RunAnimation:Stop() |
4 | elseif Humanoid.Walkspeed = = 4 then |
5 | RAnimation:Stop() |
6 | RunAnimation:Play() |
7 | end |
You didn't stop the RAnimation but you stopped the RunAnimation so it didn't work
So this is the entire script :
01 | local Player = game.Players.LocalPlayer |
02 | local Character = workspace:WaitForChild(Player.Name) |
03 | local Humanoid = Character:WaitForChild( 'Humanoid' ) |
04 |
05 | local RunAnimation = Instance.new( 'Animation' ) |
06 | RunAnimation.AnimationId = 'rbxassetid://3518952951' |
07 | RAnimation = Humanoid:LoadAnimation(RunAnimation) |
08 |
09 | if Humanoid.Walkspeed = = 25 then |
10 | RAnimation:Play() |
11 | RunAnimation:Stop() |
12 | elseif Humanoid.Walkspeed = = 4 then |
13 | RAnimation:Stop() |
14 | RunAnimation:Play() |
15 | end |
Hope I helped, thanks for reading and bye!
The easiest way would be to do the animation from a serverscript like you were doing and have it in ServerScriptService. I also want to tell you that game.Players.LocalPlayer
is only available to LocalScripts. If you think about it, the serverscript in serverscriptservice doesn't have a "LocalPlayer". Another thing, it's best to do playerInstance.Character
as this will give you the character straightup, if someone has the same name as something in the workspace, there could be errors. I wrote the code below and I'll explain it with comments.
01 | local RunAnimation = Instance.new( 'Animation' ) --Animation instance |
02 | RunAnimation.AnimationId = 'rbxassetid://3518952951' |
03 |
04 | local function check(humanoid, RAnimationReturned) |
05 | local RAnimation = humanoid:LoadAnimation(RunAnimation) --Load the animation with the characters humanoid |
06 | RAnimation.Priority = 'Action' |
07 | if humanoid.Walkspeed > 4 and RAnimatioReturned = = nil then --If their walkspeed is over 4, then play the animation |
08 | RAnimation:Play() |
09 | return RAnimation --Return the animation so it can be stopped later |
10 | elseif RAnimationReturned ~ = nil and humanoid.Walkspeed < = 4 then |
11 | RAnimationReturned:Stop() --Stop the animation, this variable is the one that was returned from an earlier execution of this function |
12 | end |
13 | end |
14 |
15 | game.Players.PlayerAdded:Connect( function (plr) --Everytime a player is added to the game |
This should work, but tell me if you're getting any errors or something isn't working right