Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago

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)
1
you should define ready out of inputbegan GoldAngelInDisguise 297 — 5y
0
put ready before you start your function tonyv537 95 — 5y

1 answer

Log in to vote
1
Answered by
BenSBk 781 Moderation Voter
5 years ago
Edited 5 years ago

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

Answer this question