Why is this always printing false?

Im trying to make a script that makes the player walk faster when they continue to walk, simular to Super Mario 64’s Walking / Running System.

But it always prints out “false” even when I am holding W, S, A, or D.

Script

local humanoid = script.Parent:FindFirstChildOfClass("Humanoid")
local holding = false

while task.wait() do
	print(holding)
	if humanoid then
		task.wait(2.2)
		if humanoid.WalkSpeed == 16 then
			if holding then
				for i = 1, 10 do
					if humanoid.WalkSpeed == 24 then
						break
					end
					
					task.wait(0.004)
					humanoid.WalkSpeed += 0.8
				end
			end
			if not holding then
				for i = 1, 10 do
					if humanoid.WalkSpeed == 16 then
						break
					end
					
					task.wait(0.004)
					humanoid.WalkSpeed -= 0.8
				end
			end
		end
	end
end

game:GetService("UserInputService").InputBegan:Connect(function(input, gpe)
	if gpe then return end
	if input then
		if input == Enum.KeyCode.W then
			holding = true
		elseif input == Enum.KeyCode.S then
			holding = true
		else
			if input == Enum.KeyCode.A or Enum.KeyCode.D then
				holding = true
			end
		end
	end
end)

game:GetService("UserInputService").InputEnded:Connect(function(input, gpe)
	if gpe then return end
	if input then
		if input == Enum.KeyCode.W then
			holding = false
		elseif input == Enum.KeyCode.S then
			holding = false
		else
			if input == Enum.KeyCode.A or Enum.KeyCode.D then
				holding = false
			end
		end
	end
end)



-- 64 B)

Instead of using a boolvalue with inputbegan and inputended, have you tried using something as:
if UserInputService:IsKeyDown(Enum.KeyCode.W) then

Its probably because on any of your inputended/keyup for any of the keys W,S,A.D you are setting it to false
you might want to make sure none of the keys are being held down then set to false

if game:GetService("UserInputService"):IsKeyDown(keyCode.W or keyCode.S or keyCode.A or keyCode.D) then
	holding = true
else
	holding = false
end

This doesn’t seem to work, it only prints false.

@Nyonic | @RasherTheGamer ( Sorry for ping )

You can’t just go the route of IsKeyDown(KeyCode or), you have to make one function for each.
For example,
if game:GetService("UserInputService"):IsKeyDown(Enum.KeyCode.W) or game:GetService("UserInputService"):IsKeyDown(Enum.KeyCode.A) or game:GetService("UserInputService"):IsKeyDown(Enum.KeyCode.S) or game:GetService("UserInputService"):IsKeyDown(Enum.KeyCode.D) then holding = true

2 Likes

@RasherTheGamer is right but I would rather use something like this:

local UserInputService = game:GetService('UserInputService')
local KEYS = {Enum.KeyCode.W, Enum.KeyCode.S, Enum.KeyCode.A, Enum.KeyCode.D}

local function isKeyDown()
    for _, key in KEYS do
        if UserInputService:IsKeyDown(key) then
            return true
        end
    end
    return false
end
1 Like

result ( local result = isKeyDown() ) always returns to false…

Sorry, i pasted wrong code with a mistake, I edited previous post so try again.