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

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

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.

2 comments

0

I would recommend using bool values.

0

UserInputService will never work in server scripts

2 answers

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.

1 comment

0

Your loop can be shortened to "while keydown do"

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)

4 comments

0

wait() should NOT be a condition in while loops.

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.

0

incapz means you should not put wait() in the condition of while loops

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.

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