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

How come this function plays even though it can only play when "ready = true", but ready is false?

It's only supposed to enhance your walkspeed when ready = true, but it seems like ready is always true, even though there's a line to change it to false for 8 seconds.

uis.InputBegan:connect(function(input,process) -- Sprint
local ready = true
if not process and input.KeyCode == Enum.KeyCode.G and ready == true then
ready = false
player.Character.Humanoid.WalkSpeed = player.Character.Humanoid.WalkSpeed + 14
wait (3)
player.Character.Humanoid.WalkSpeed = player.Character.Humanoid.WalkSpeed - 14
wait(5)
ready = true
end
end)

2 comments

1

you should define ready out of inputbegan

0

put ready before you start your function

1 answer

✓ Accepted answer

You are declaring and assigning to true a new local variable, ready, every time an input begins. Later in the listening function, when you attempt to check the value of this variable, it will be true.

To fix your script, you need to declare and initialise ready outside of the listening function. You should also be using RBXScriptSignal:Connect instead of RBXScriptSignal:connect, as the latter is deprecated. Along with this, I've made some other changes to your script that account for cases where the character may not exist, etc. and improve performance to a minor degree:

local user_input_service = game:GetService("UserInputService")
local is_ready = true

user_input_service.InputBegan:Connect(function(input, is_game_processed)
	-- Check if G key wasn't pressed.
	if input.KeyCode ~= Enum.KeyCode.G then
		return
	end
	-- Check if input was game processed.
	if is_game_processed then
		return
	end
	-- Check if not ready.
	if not is_ready then
		return
	end
	-- Check if character doesn't exist.
	local character = player.Character
	if not character then
		return
	end
	-- Check if Humanoid doesn't exist.
	local humanoid = character:FindFirstChild("Humanoid")
	if not humanoid then
		return
	end
	-- Execute code.
	is_ready = false
	humanoid.WalkSpeed = humanoid.WalkSpeed + 14
	wait(3)
	humanoid.WalkSpeed = humanoid.WalkSpeed - 14
	wait(5)
	is_ready = true
end)

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