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

how on earth do i make a player change animation when it hits a walkspeed? (UNANSWERED)

Asked by 5 years ago
Edited 5 years ago

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.

01local Player = game.Players.LocalPlayer
02    local Character = workspace:WaitForChild(Player.Name)
03        local Humanoid = Character:WaitForChild('Humanoid')
04 
05local RunAnimation = Instance.new('Animation')
06RunAnimation.AnimationId = 'rbxassetid://3518952951'
07RAnimation = Humanoid:LoadAnimation(RunAnimation)
08 
09if Humanoid.Walkspeed == 25 then
10    RAnimation:Play()
11elseif Humanoid.Walkspeed == 4 then
12    RunAnimation:Stop()
13end

can someone help me please?

(it is a script not a localscript, the script is located in serverscriptservice)

0
Are you sure the script is not inside a player? Does the script clone itself into players? Qariter 110 — 5y
0
Title changed XD Xapelize 2658 — 5y
0
For this script to work, the actual WalkSpeed has to change, not the player's velocity per say. The below scripts should work as expected, if the property itself changes. If you want to see how fast a player is currently moving, that's something else Shawnyg 4330 — 5y

3 answers

Log in to vote
0
Answered by
Qariter 110
5 years ago
Edited 5 years ago

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'
05RAnimation = Humanoid:LoadAnimation(RunAnimation)
06 
07if Humanoid.Walkspeed == 25 then
08    RAnimation:Play()
09elseif Humanoid.Walkspeed == 4 then
10    RAnimation:Stop()
11end

Your script was nearly correct, but it was just in the wrong place. Unless it was getting cloned into players.

Edit: Made a mistake.

0
i want all the players to see the persons animation, not just the client, didnt work but upvote for you for trying dragonlolpp2 2 — 5y
0
@dragonlolpp2 animations from the client replicate to the server. BlackOrange3343 2676 — 5y
Ad
Log in to vote
0
Answered by
Xapelize 2658 Moderation Voter Community Moderator
5 years ago

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 :

1if Humanoid.Walkspeed == 25 then
2    RAnimation:Play()
3    RunAnimation:Stop()
4elseif Humanoid.Walkspeed == 4 then
5    RAnimation:Stop()
6    RunAnimation:Play()
7end

You didn't stop the RAnimation but you stopped the RunAnimation so it didn't work

So this is the entire script :

01local Player = game.Players.LocalPlayer
02    local Character = workspace:WaitForChild(Player.Name)
03        local Humanoid = Character:WaitForChild('Humanoid')
04 
05local RunAnimation = Instance.new('Animation')
06RunAnimation.AnimationId = 'rbxassetid://3518952951'
07RAnimation = Humanoid:LoadAnimation(RunAnimation)
08 
09if Humanoid.Walkspeed == 25 then
10    RAnimation:Play()
11    RunAnimation:Stop()
12elseif Humanoid.Walkspeed == 4 then
13    RAnimation:Stop()
14    RunAnimation:Play()
15end

Hope I helped, thanks for reading and bye!

0
oop, we posted awnsers at the same time x) Qariter 110 — 5y
Log in to vote
0
Answered by
sheepposu 561 Moderation Voter
5 years ago
Edited 5 years ago

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.

01local RunAnimation = Instance.new('Animation') --Animation instance
02RunAnimation.AnimationId = 'rbxassetid://3518952951'
03 
04local 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
13end
14 
15game.Players.PlayerAdded:Connect(function(plr) --Everytime a player is added to the game
View all 21 lines...

This should work, but tell me if you're getting any errors or something isn't working right

0
09:20:43.393 - ServerScriptService.Script:16: attempt to index field 'Character' (a nil value) dragonlolpp2 2 — 5y
0
the error is at line 16 dragonlolpp2 2 — 5y
0
Try putting "repeat wait() until plr.Character" before line 16, so it will be the new line 16 sheepposu 561 — 5y
0
sorry for long response but i did not get any errors and it seems to not work dragonlolpp2 2 — 5y
View all comments (2 more)
0
put prints inside the all the functions and have them print different things to see if everything is running smoothly sheepposu 561 — 5y
0
I think I found the problem, I edited the code, now try it out sheepposu 561 — 5y

Answer this question