Why doesn't my walk toggle work?

I want the player to walk when they press Z. This is a local script in StarterCharacterScripts. I can’t find anything online that helps me solve this issue.

local humanoid = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")

local walking = false

local function walk(inputObject, gameProcessedEvent)
	if inputObject.KeyCode == Enum.KeyCode.Z then
		walking = not walking
		if walking == true then
			humanoid.WalkSpeed = 8
		else
			humanoid.WalkSpeed = 16
		end
	end
end

game:GetService("UserInputService").InputEnded:Connect(walk)
4 Likes

Is there anything in the console (errors)? If not, try adding print statements for debug purposes.

Also, is there any reason why you’re not using InputBegan?

3 Likes

Is there anything in the console (errors)? If not, try adding print statements for debug purposes.
Here’s the script now:

print(1)local humanoid = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")
print(2, humanoid.WalkSpeed)
print(3)local walking = false
print(4)
print(5)local function walk(inputObject, gameProcessedEvent)
print(6)	if inputObject.KeyCode == Enum.KeyCode.Z then
print(7)		walking = not walking
print(8, walking)		if walking == true then
print(9, walking)			humanoid.WalkSpeed = 8
print(10, walking)		else
print(11, walking, humanoid.WalkSpeed)			humanoid.WalkSpeed = 16
print(12, walking, humanoid.WalkSpeed)		end
print(13, walking, humanoid.WalkSpeed)	end
print(14, walking, humanoid.WalkSpeed)end
print(15)
print(16)game:GetService("UserInputService").InputBegan:Connect(walk)

1, 2, 3, 4, 5, 15, and 16 are printed immediately, 2 with the expected value of 16.
6 and 14 are printed on any button press. Pressing Z changes if walking is true or false but walkspeed stays the same.
6, 7, 8, 11, 12, 13, and 14 are printed when I press Z. walking is shown to be true and walkspeed is 8.

Also, is there any reason why you’re not using InputBegan?
Can’t remember exactly where, but the documentation I was looking at used InputEnded. I changed it to InputBegan because that makes more sense.

2 Likes

i copy and pasted the local script and it worked just fine for me :person_shrugging: (video please?)

3 Likes

Is there anything else in your game, server or client, that changes your walkspeed?

2 Likes

this is interesting, i have a different script to manage spriniting, and disabling that one makes it work.
edit: the sprinting one always works

@TheSai_ki

Here’s the code for the sprinting one:

local player = game.Players.LocalPlayer
local character = player.Character
local humanoid = character:WaitForChild("Humanoid")

local sprinting = false

game:GetService("UserInputService").InputBegan:Connect(function(inputObject, gameProcessedEvent)
	if inputObject.KeyCode == Enum.KeyCode.LeftShift then
		sprinting = true
	end
end)

game:GetService("UserInputService").InputEnded:Connect(function(inputObject, gameProcessedEvent)
	if inputObject.KeyCode == Enum.KeyCode.LeftShift then
		sprinting = false
	end
end)

while true do
	wait(0.1)
	if sprinting then
		humanoid.WalkSpeed = 24 
	else
		humanoid.WalkSpeed = 16
	end
end

post cant be empty post cant be empty

2 Likes

I see the problem, the sprinting script is completely overriding the walking script, I recommend combining the two scripts into one.

(Also, you might want to use RunService’s Heartbeat instead of while true do)

2 Likes

how did i overlook this :sob: THANK YOU

edit: okay, this works perfectly now. i dont know if this was a good or bad idea, but this is what I have now:

local humanoid = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")

local walking = false

---WALKING

local function walk(inputObject, gameProcessedEvent)
	if inputObject.KeyCode == Enum.KeyCode.Z then
		walking = not walking
		if walking == true then
			humanoid.WalkSpeed -=8
		else
			humanoid.WalkSpeed +=8
		end
	end
end

game:GetService("UserInputService").InputBegan:Connect(walk)

---SPRINTING

game:GetService("UserInputService").InputBegan:Connect(function(inputObject, gameProcessedEvent)
	if inputObject.KeyCode == Enum.KeyCode.LeftShift then
		humanoid.WalkSpeed += 8
	end
end)

game:GetService("UserInputService").InputEnded:Connect(function(inputObject, gameProcessedEvent)
	if inputObject.KeyCode == Enum.KeyCode.LeftShift then
		humanoid.WalkSpeed -= 8
	end
end)

I decided to add to/subtract from the walkspeed instead of setting the value to something else to prevent breakage from pressing both keys at once

1 Like
local player = game.Players.LocalPlayer
local character = player.Character
local humanoid = character:WaitForChild("Humanoid")

local sprinting = false
local walking = false

game:GetService("UserInputService").InputBegan:Connect(function(inputObject, gameProcessedEvent)
	if inputObject.KeyCode == Enum.KeyCode.LeftShift then
		sprinting = true
		walking = false
	elseif inputObject.KeyCode == Enum.KeyCode.Z then
		walking = true
	end
end)

game:GetService("UserInputService").InputEnded:Connect(function(inputObject, gameProcessedEvent)
	if inputObject.KeyCode == Enum.KeyCode.LeftShift then
		sprinting = false
	elseif inputObject.KeyCode == Enum.KeyCode.Z then
		walking = false
	end
end)

game:GetService("RunService").Heartbeat:Connect(function()
	if sprinting then
		humanoid.WalkSpeed = 24 
	elseif walking then
		humanoid.WalkSpeed = 8
	else
		humanoid.WalkSpeed = 16
	end
end)

Here’s an idea for combining the scripts

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.