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

How do i have something loop until the player stops pressing a key?

Asked by 5 years ago

This is the first script (localsript) which works perfectly

local player = game.Players.LocalPlayer
local UIS = game:GetService("UserInputService")
local deboune = false

UIS.InputBegan:Connect(function(Input)

    if Input.KeyCode == Enum.KeyCode.LeftShift then
        if deboune == true then return end
        deboune = true

            game.ReplicatedStorage.Controls.ShiftCharge:FireServer(Input)
            wait(.3)
            deboune = false

    end
end)

this is the script in which im having some trouble with. i got rid of lots of the script to shorten it to the important bits

local user = game:GetService("UserInputService")

game.ReplicatedStorage.Controls.ShiftCharge.OnServerEvent:Connect(function(plr, Input)

repeat
        x = x + 1
        wait(1)
    until user.InputEnded --Input.UserInputState == Enum.UserInputState.End

end)

Input.UserInputState == Enum.UserInputState.End

works in studio as there is no client-server boundary but in game it doesnt work.

user.InputEnded doesnt work in studio and the game eventhough im still pressing the key.

0
I would recommend using bool values. B_rnz 171 — 5y
0
UserInputService will never work in server scripts aazkao 787 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago

Sorta interesting, however here is a way you can do it (Keep in mind these need to be local script)

local player = game.Players.LocalPlayer
local UIS = game:GetService("UserInputService")
local keydown = false



UIS.InputBegan:Connect(function(Input)

    if Input.KeyCode == Enum.KeyCode.LeftShift then
       keydown = true
    end
end)

UIS.InputEnded:Connect(function(Input)
    if Input.KeyCode == Enum.KeyCode.LeftShift then
        keydown = false
    end
end

while true do
wait(1)
if keydown then
-- whatever you want to repeat while the key is down
end
end

Here is how it works: When the input starts, it sets a variable to true. The loop that runs then realizes that a key is down and runs. When the key stops being held down, it changes the variable.

0
Your loop can be shortened to "while keydown do" User#19524 175 — 5y
Ad
Log in to vote
0
Answered by
B_rnz 171
5 years ago

I tried doing this using bool values so whenever it equals true, the value starts adding. Just as Commander was explaining.

Local script:

local player = game.Players.LocalPlayer
local UIS = game:GetService("UserInputService")
local deboune = false

UIS.InputBegan:Connect(function(Input)

    if Input.KeyCode == Enum.KeyCode.LeftShift then
        if deboune == true then return end
        deboune  = true
        player.Clicking.Value = true

            game.ReplicatedStorage.Controls.ShiftCharge:FireServer(Input)
            wait(.3)
            deboune = false
    end
    player.Clicking.Value = false

end)

Local script:

local user = game:GetService("UserInputService")
local plr = game.Players.LocalPlayer

-- Creating bool value
local Boolvalue = Instance.new("BoolValue")
Boolvalue.Parent = plr
Boolvalue.Name = "Clicking"

local NumberValue = Instance.new("NumberValue")
NumberValue.Parent = plr
NumberValue.Name = "Example"
NumberValue.Value = 0

game.ReplicatedStorage.Controls.ShiftCharge.OnServerEvent:Connect(function(plr)

    while wait(.2) do
        spawn(function()
            if Boolvalue.Value == true then
                plr.Example.Value = plr.Example.Value + 1
            end
        end)
    end

end)
0
wait() should NOT be a condition in while loops. User#19524 175 — 5y
0
It worked for me, when I made a running simulator that I haven't released. I added wait because once Left Shift is clicked, it goes crazy. Causing immense lag and crashes. B_rnz 171 — 5y
0
incapz means you should not put wait() in the condition of while loops aazkao 787 — 5y
0
Ohh, I understand now. I used that while the humanoid.Running was true. I just tested out the script now, It doesn't keep doing it when player is HOLDING down the button, my bad. B_rnz 171 — 5y

Answer this question