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

How to change a players movement speed?

So I have a script that I made and put into ServerScriptService. It is suppoed to first, create the variable "PlrBlk". Next it is supposed to use the name of the play stored in PlrBlk and find the player in the workspace. That is asigned to the variable "plr". The variable humanoid represents "Humanoid", found inside plr. Back to line 3. The error is that the value of plr is nil and I can't figure out why. Thanks in Advance

game.Players.PlayerAdded:Connect(function(player)
	local PlrBlk = player.Name
	local plr = workspace:FindFirstChild(PlrBlk)
	local humanoid = plr:FindFirstChild("Humanoid")
	humanoid.Walkspeed = 0
end)

2 answers

✓ Accepted answer

you could just index the Character property of the player object instead, though you'd need to listen for the CharacterAdded event since the character probably didnt load yet once PlayerAdded was fired. then use :WaitForChild() on the humanoid, since it also might have not loaded yet and the property is called WalkSpeed not Walkspeed. also dont use :FindFirstChild() just to directly index the object like what you'd use with the . operator; use it to check if a object exists or not.

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local humanoid = character:WaitForChild("Humanoid")

		humanoid.WalkSpeed = 0
	end)
end)

1 comment

0

Oh, I see thx

The best idea is just out a local script inside of StarterCharacterScripts that says this.

repeat wait() until script.Parent.Humanoid ~= nil

script.Parent.Humanoid.WalkSpeed = 20

1 comment

1

That way there is much simpler and works every time

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