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

Why my Humanoid.WalkSpeed won't change?

I have this problem. I can't change it with codes! He doesn't send me any error too. These is my code.

local Player = game.Players.LocalPlayer
local Character = Player.Character.Humanoid
local WalkSpeed = Character.WalkSpeed
local ControlHolding = false
local MoveHolding = false

local Animation = Instance.new("Animation")
Animation.AnimationId = "http://www.roblox.com/asset/?id=1909808255"
local Animation2 = Instance.new("Animation")
Animation2.AnimationId = "http://www.roblox.com/asset/?id=1909810268"
local Squating = Character:LoadAnimation(Animation)
local SquatMoving = Character:LoadAnimation(Animation2)
local IdleAnimation = Character:LoadAnimation(game.Players.LocalPlayer.Character.Animate.idle.Animation1)
local WalkAnimation = Character:LoadAnimation(game.Players.LocalPlayer.Character.Animate.walk.WalkAnim)
Squating.Looped = true
SquatMoving.Looped = true

game:GetService('UserInputService').InputBegan:Connect(function(input,IsTyping)
	if IsTyping then return end
	local KeyPressed = input.KeyCode
	if KeyPressed == Enum.KeyCode.LeftControl then
		ControlHolding = true
		WalkSpeed = 0 --This! Also i printed it, he shows me 0 too.
		if MoveHolding == true then
			WalkAnimation:stop()
			IdleAnimation:stop()
			SquatMoving:play()
		else
			SquatMoving:stop()
			WalkAnimation:stop()
			IdleAnimation:stop()
			Squating:play()
		end
	end

function onKeyRelease(inputObject,gameProcessed)
	if inputObject.KeyCode == Enum.KeyCode.LeftControl then
		ControlHolding = false
		Squating:stop()
		SquatMoving:stop()
		WalkSpeed = 15 --Here gets problem too maybe?
	end
end
game:GetService("UserInputService").InputEnded:connect(onKeyRelease)

Yes i actually making a squating script, and i just can't change player's speed! I want to know why. By the way this is not all my codes. And Thanks for helping!

2 answers

✓ Accepted answer

When you set a variable to a property, it doesn't set a reference to the property, only records what it is at that specific time. So your WalkSpeed variable only has the value of 16 (or whatever number the WalkSpeed is), rather than the property itself. To solve this, just add the .WalkSpeed on to your Character variable whenever you want to change it.

So when setting the WalkSpeed to 0:

Character.WalkSpeed = 0

rather than

WalkSpeed = 0

Also, in a game server, the character takes a while to load. To account for this, you can use WaitForChild and CharacterAdded:

local Player = game.Players.LocalPlayer
local CharacterModel = Player.Character or Player.CharacterAdded:Wait()
local Character = CharacterModel:WaitForChild("Humanoid")

4 comments

0

Ooooh! So i can't local it right?

0

Making it into a variable will just store the value, so no you can't directly make properties into variables

0

Okay! i'm gonna try it.

0

It's WORKED! Thank you for help!!

WalkSpeed is the variable not the walkspeed of the humanoid meaning you only set the variable and not the walkspeed of the humanoid.

All you need to do is set the WalkSpeed property.

local Player = game:GetService('Players').LocalPlayer -- best to use get service
local Character = Player.Character or Player.CharacterAdded:Wait() -- the player can take time to load in so get the player character if it or wait for it if the character is nil
local Humanoid = Character:WaitForChild('Humanoid')

.... 

game:GetService('UserInputService').InputBegan:Connect(function(input)
	if KeyPressed == Enum.KeyCode.LeftControl then
		Humanoid.WalkSpeed = 0 -- set walk speed
	end
end)

game:GetService("UserInputService").InputEnded:Connect(function(input)
	if KeyPressed == Enum.KeyCode.LeftControl then
		Humanoid.WalkSpeed = 16 -- set back
	end
end)

I hope this helps.

3 comments

0

You teach me another things! thanks! If i can accept two ansers xD.

0

But he answer my question, so i'll accept his answer.

0

thats ok

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