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)
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.
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
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
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)