Still condition function

Is there a function / method or anything that can be made into a function that will detect if the condition is still true, or if the correctness of the condition has changed, then the function is interrupted (does not stop)? like

local holding=true
local mousebutton=false



mouse.MouseButton1Down.Connect:Function()
   mousebutton=true
end)

mouse.MouseButton1Up.Connect:Function()
   mousebutton=false
end)



randomfunction:Connect(function()
    if mousebutton then 
       wait(4)
    if 'mousebutton still true and not and didnt changed' then
       print('stillTrue')
    end
    if 'mousebutton changed' then
       print('changed')
    end
end) 

It probably to be done through “while something do” and “break”

make a variable for a current value and check if the value has changed using that variable for example:

local number = 4
local currentNumber = number
number += 1
if currentNumber == number then
    print("value hasnt changed")
else
    print("value has changed")
end

while mousebutton do
end

that loop will run until mousebutton is set to false

but what if I need to execute a function if the value has changed? for example, to defuse a bomb, you need to hold down the button for 4 seconds, when the person presses the button, the function is performed, and vice versa when the button is released during a defusing, another function is performed. I know it’s a very simple script but I don’t really remember how to do it :frowning: button press and

you could add a bool value inside a script and script this:

script.BoolValue:GetPropertyChangedSignal(“Value”):Connect(function()
if script.BoolValue.Value == true then
else
if script.BoolValue.Value == false then
end
end
end)

1 Like

Use your holding variable! Overlapping states requires more variables

local holding=false
local mousebutton=false

mouse.MouseButton1Down.Connect:Function()
   mousebutton=true
end)

mouse.MouseButton1Up.Connect:Function()
   mousebutton=false
   holding = false
end)

randomfunction:Connect(function()
    if mousebutton then
       holding = true
       wait(4)
       if mousebutton and holding then
          print('stillTrue')
       else
          print('changed')
       end
   end
end) 

is it possible to do it in some other way? I’m trying to make cheat protection but I have too many remote events and all sorts of values ​​that help create cheats. all the same, this is not so important, but it will still be good if there is another way

but there will be a bug when the player pressed the button, released it, waited 3.5 seconds, pressed it again and the function worked, because 4 seconds have passed and since nothing was checked before

But in my example “holding” is only set to true at the start of the randomFunction, as long as the random function does not start again, releasing the button will fail the following if statement after 4 seconds.

1 Like

Oh, didn’t notice. Thank you very much, I probably finally solved almost half of the problems in my obfuscated scripts

1 Like

If you prefer an event driven approach as opposed to a state dependent one you can use the following.

local Enumeration = Enum
local Game = game
local UserInputService = Game:GetService("UserInputService")

local function OnInputBegan(InputObject, GameProcessed)
	if GameProcessed then return end
	if InputObject.UserInputType == Enumeration.UserInputType.MouseButton1 then
		local Time = 4
		repeat
			Time -= task.wait()
			if UserInputService:IsMouseButtonPressed(Enumeration.UserInputType.MouseButton1) then
				--Do code.
			else
				--Do code.
			end
		until Time < 0
	end
end

UserInputService.InputBegan:Connect(OnInputBegan)
1 Like