Slowly increment value while holding down proximity prompt

  1. What do you want to achieve? a variable that increments for every second that you hold down a ProximityPrompt

  2. What is the issue? I don’t know how to do this in a clean effective way.

  3. What solutions have you tried so far? I do have one way to do this but the way that I currently have is very messy and I don’t like it, here is the way I’m currently doing stuff:

local isDone

ProximityPrompt.PromptButtonHoldBegan:Connect(function()
    isDone = false

    repeat
        value += 1
        wait(1)
    until isDone
end)

ProximityPrompt.PromptButtonHoldEnded:Connect(function()
    isDone = true
end)

is there a more clean way to do this, or is it possible to do something like
repeat code until ProximityPrompt.PromptButtonHoldEnded:Wait()

tldr; I want to increment a value for every second that a player holds down a ProximityPrompt

1 Like

You can probably do something like this:

local pressed
local heldFor

ProximityPrompt.PromptButtonHoldBegan:Connect(function()
    heldFor = os.time()
    pressed = true

    while pressed do
        if not ProximityPrompt.PromptButtonHoldEnded:Wait() then
            heldFor = (os.time() - heldFor)
        else
            pressed = false
            return
        end
    end
end)

Your code to me seems fine, though. But this way that I’m doing is just using os.time() to change the value, and listening for when the player stops holding the prompt.

basically what you was saying.

2 Likes

what I mean is I don’t like how chunky the code was. I want something that’s small and doesn’t need variables to check if your pressing the prompt. though I might be asking for too much.

1 Like
ProximityPrompt.PromptButtonHoldBegan:Connect(function()
	local Status = false
	ProximityPrompt.PromptButtonHoldEnded:Once(function()
		Status = true
	end)
	
	repeat
		Value += 1
		task.wait(1)
	until Status
end)

is the smallest u can reasonably make this unfortunately

3 Likes

Why do you want it smaller when your original code gets the job done + you can easily tell what everything does and is supposed to do?

1 Like

cause I’m picky and like compact code lol, but I don’t need it to be compact so I guess I’ll just settle for the old code.

1 Like

Just like what you have now because eventually it would be soo compact you could never understand what it is and it is a small thing

2 Likes
local Script = script
local Prompt = Script.Parent

local Value = 0

local function OnPromptButtonHoldBegan(BeganPlayer)
	local Connection
	local function OnPromptButtonHoldEnded(EndedPlayer)
		if BeganPlayer == EndedPlayer then Connection:Disconnect() end
	end
	
	Connection = Prompt.PromptButtonHoldEnded:Connect(OnPromptButtonHoldEnded)
	while Connection.Connected do
		task.wait(1)
		Value += 1
	end
end

Prompt.PromptButtonHoldBegan:Connect(OnPromptButtonHoldBegan)
1 Like

hold on, may I ask what the :Once does?


Basically connects and then once its fired disconnects automatically