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 4 years ago
Edited 4 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.

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)

0
Are you sure the script is not inside a player? Does the script clone itself into players? Qariter 110 — 4y
0
Title changed XD Xapelize 2658 — 4y
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 — 4y

3 answers

Log in to vote
0
Answered by
Qariter 110
4 years ago
Edited 4 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.

      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.

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 — 4y
0
@dragonlolpp2 animations from the client replicate to the server. BlackOrange3343 2676 — 4y
Ad
Log in to vote
0
Answered by
Xapelize 2658 Moderation Voter Community Moderator
4 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 :

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!

0
oop, we posted awnsers at the same time x) Qariter 110 — 4y
Log in to vote
0
Answered by
sheepposu 561 Moderation Voter
4 years ago
Edited 4 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.

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

0
09:20:43.393 - ServerScriptService.Script:16: attempt to index field 'Character' (a nil value) dragonlolpp2 2 — 4y
0
the error is at line 16 dragonlolpp2 2 — 4y
0
Try putting "repeat wait() until plr.Character" before line 16, so it will be the new line 16 sheepposu 561 — 4y
0
sorry for long response but i did not get any errors and it seems to not work dragonlolpp2 2 — 4y
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 — 4y
0
I think I found the problem, I edited the code, now try it out sheepposu 561 — 4y

Answer this question