Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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)

3 comments

0

Are you sure the script is not inside a player? Does the script clone itself into players?

0

Title changed XD

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

3 answers

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.

2 comments

0

i want all the players to see the persons animation, not just the client, didnt work but upvote for you for trying

0

@dragonlolpp2 animations from the client replicate to the server.

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!

1 comment

0

oop, we posted awnsers at the same time x)

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

6 comments

0

09:20:43.393 - ServerScriptService.Script:16: attempt to index field 'Character' (a nil value)

0

the error is at line 16

0

Try putting "repeat wait() until plr.Character" before line 16, so it will be the new line 16

0

sorry for long response but i did not get any errors and it seems to not work

0

put prints inside the all the functions and have them print different things to see if everything is running smoothly

0

I think I found the problem, I edited the code, now try it out

This discussion is preserved as it was. New answers, comments, and votes are closed.