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

How to make a value decrease while mouse is holding?

Hey, so I have been trying to make a script that decreases a variable's value while the mouse is holding button 1, the mouse section was easy, but I can't find a way to make the value decrease when mouse is holding, While true do keep decreasing and it won't stop, the minimum value is 0, can someone help?

local Mouse = player:GetMouse()
local value = 100 -- value supposed to reduce when mouse is being hold

function OnHoldRapid()
	
end
Mouse.Button1Down:Connect(OnHoldRapid)

function OnFree()

end
Mouse.Button1Up:Connect(OnFree)

Thank you!

6 comments

0

I'm not sure on this, so feel free to correct me on this anyone, but I believe Button1Down would only count 1 time on 1 "hold".

0

hey robloxknight2000, Button1Down is supposed to trigger a functions that keep repeating, but i don't know how i would stop it, so i guessed i would need a Button1Up to stop it or something...

0

Yeah, judging by the DevHub article on it, and their examples, it'll only fire once, meaning it won't be able to decrease to 0. https://developer.roblox.com/api-reference/event/Mouse/Button1Down

0

i have no idea on how to do this then...

0

You could try making a loop?

0

how exactly?

1 answer

✓ Accepted answer

Hello, to achieve this, do the following:

mouse = game.Players.LocalPlayer:GetMouse() --Getting mouse
value = 100 --Setting a starting value

local Loopend = false --How we will stop the loop
mouse.Button1Down:Connect(function() --Detecting mouseDown
	repeat --continuously deducting from the value until Loopend = true
		value = value - 1
		wait(1)
	until
		Loopend = true
	Loopend = false
end)

mouse.Button1Up:Connect(function() --Detecting the button up and sending the signal to stop the loop
	Loopend = true 
end)

3 comments

0

Thank you! I didn't know it was that simple ;p

0

Can i use until to set a minimum limit too like until value = 0 ?

0

Sure thing. I didnt make it, but this is as simple as you can get in terms of what you want!

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