How can I make Spam Keybind System

I am trying to make the Spam Keybind System. Something like this :

Here’s my script [Just incase] :


local pressing = false

UIS.InputBegan:Connect(function(input)
		
		if input.KeyCode == Enum.KeyCode.Space then
			
			value.Value = value.Value + 0.122
			bar:TweenSize(UDim2.new(value.Value, 0, 1, 0), 'Out', 'Quint', .1, true)
			
			pressing = true
			
			while value.Value < 1 and pressing do
				
				value.Value = value.Value - 0.01
				bar:TweenSize(UDim2.new(value.Value, 0, 1, 0), 'Out', 'Quint', .1, true)

				if value.Value < 0 then
					value.Value = 0
					bar:TweenSize(UDim2.new(0, 0, 1, 0), 'Out', 'Quint', .1, true)
				end

				if value.Value >= 1 then
					event:FireServer()
					guis.Enabled = false
					break
				end
				
				task.wait(0.15)
			end
		end
	end)

I understand that putting the while loop in the InputBegan is a bad idea. Whenever I press it, it will start a new loop which will compete with the other already starting loop. How can I make the Spam Keybind System? [ I am making it go down whenever the player stops pressing the space bar ]

3 Likes

swap some things out

local pressing = true

function Start()
while value.Value < 1 and pressing do
				
		value.Value = value.Value - 0.01
		bar:TweenSize(UDim2.new(value.Value, 0, 1, 0), 'Out', 'Quint', .1, true)

			if value.Value < 0 then
				value.Value = 0
				bar:TweenSize(UDim2.new(0, 0, 1, 0), 'Out', 'Quint', .1, true)
			end

			if value.Value >= 1 then
				event:FireServer()
				guis.Enabled = false
				break
			end
				
			task.wait(0.15)
		end
	end
end

local StartThread = corontine.create(Start)
corontine.resume(StartThread)

UIS.InputBegan:Connect(function(input)
		
		if input.KeyCode == Enum.KeyCode.Space then
			
			value.Value = value.Value + 0.122
			bar:TweenSize(UDim2.new(value.Value, 0, 1, 0), 'Out', 'Quint', .1, true)
			
			pressing = true
			
	end)
1 Like

Had to make a few modification, but I didn’t think about using coroutine :pray: Thank you bro

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.