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.
local Player = game.Players.LocalPlayer local Character = workspace:WaitForChild(Player.Name) local Humanoid = Character:WaitForChild('Humanoid') local RunAnimation = Instance.new('Animation') RunAnimation.AnimationId = 'rbxassetid://3518952951' RAnimation = Humanoid:LoadAnimation(RunAnimation) if Humanoid.Walkspeed == 25 then RAnimation:Play() elseif Humanoid.Walkspeed == 4 then RunAnimation:Stop() 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.
local Humanoid = script.Parent:FindFirstChild("Humanoid") -- check for humanoid inside player local RunAnimation = Instance.new('Animation') RunAnimation.AnimationId = 'rbxassetid://3518952951' RAnimation = Humanoid:LoadAnimation(RunAnimation) if Humanoid.Walkspeed == 25 then RAnimation:Play() elseif Humanoid.Walkspeed == 4 then RAnimation:Stop() 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 :
if Humanoid.Walkspeed == 25 then RAnimation:Play() RunAnimation:Stop() elseif Humanoid.Walkspeed == 4 then RAnimation:Stop() RunAnimation:Play() end
You didn't stop the RAnimation but you stopped the RunAnimation so it didn't work
So this is the entire script :
local Player = game.Players.LocalPlayer local Character = workspace:WaitForChild(Player.Name) local Humanoid = Character:WaitForChild('Humanoid') local RunAnimation = Instance.new('Animation') RunAnimation.AnimationId = 'rbxassetid://3518952951' RAnimation = Humanoid:LoadAnimation(RunAnimation) if Humanoid.Walkspeed == 25 then RAnimation:Play() RunAnimation:Stop() elseif Humanoid.Walkspeed == 4 then RAnimation:Stop() RunAnimation:Play() 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.
local RunAnimation = Instance.new('Animation') --Animation instance RunAnimation.AnimationId = 'rbxassetid://3518952951' local function check(humanoid, RAnimationReturned) local RAnimation = humanoid:LoadAnimation(RunAnimation) --Load the animation with the characters humanoid RAnimation.Priority = 'Action' if humanoid.Walkspeed > 4 and RAnimatioReturned == nil then --If their walkspeed is over 4, then play the animation RAnimation:Play() return RAnimation --Return the animation so it can be stopped later elseif RAnimationReturned ~= nil and humanoid.Walkspeed <= 4 then RAnimationReturned:Stop() --Stop the animation, this variable is the one that was returned from an earlier execution of this function end end game.Players.PlayerAdded:Connect(function(plr) --Everytime a player is added to the game local humanoid = plr.Character:WaitForChild('Humanoid') local RAnimation = check(humanoid, nil) --Play the function once to start up everything humanoid:GetPropertyChangedSignal('Walkspeed'):Connect(function() --Anytime the player's walkspeed changes, do the check function RAnimation = check(humanoid, RAnimation) end) end)
This should work, but tell me if you're getting any errors or something isn't working right