Checking if a variable changed

Basically, i have a setTime like 4 secs or smth. what i want to do is to wait for setTime's value such as wait for 4 secs. Then do sum stuff. Iam facing an issue tho.

I am using an if statement to check if a variable is tru or not and if it is it runs the wait(setTime) line. But i want to break the if statement value is changed mid-waiting.

So is there any better way to wait other than using the wait(). And also how do i check if the var is changed?

Code:

UserInputService.InputBegan:Connect(function(inputObject)
	if inputObject.UserInputType == Enum.UserInputType.MouseButton1 then
        breaking = true
        local secs = 4 --time to wait
	       if breaking then
	           wait(secs)
		      --do stuff here
end)

UserInputService.InputEnded:Connect(function(inputObject)
	if inputObject.UserInputType == Enum.UserInputType.MouseButton1 then
		breaking = false
end)

You could just check if the condition is still true after the waiting time.

breaking = true
task.wait(4)
if breaking then
     -- do stuff
end

But will it work, lets say while iam like 0.007 secs into breaking the block and then i leave my leftmousebtn. Will it still check?

yes because the variable being checked is global (outside the connection).
So if it changes before the wait time is up, when you re-check for the variable it will see that it is false and not true.

why task.wait tho?Can u explain the use of it? why use it instead of putting wait in the if statement?

And also i want to wait inside the if statement thts y i asked if there is a diferrent way to do it.

Task Library

Eventually I think wait() will become deprecated for this instead. I suggest using task.wait() in the future.

What iam trying to say is that i want to count from the time like 4 secs, if mid counting the var changed i want to break the if satatement? Any solutions?

I would just do what @Dev_Ryan said. I thought you already had your solution.

You could do a while true do loop and check while breaking is true do your code, then break at the very end so it doesn’t repeat.

Nvm abt the wait issue i solved it but it still doesnt stop the if statement even tho breaking is false.

InputBegan:

if input.UserInputType == Enum.UserInputType.MouseButton1 then
	if not breaking then
		breaking = true
		local timeSpent = 0
		repeat
			timeSpent += 1
			task.wait(1)
		until not breaking or timeSpent >= 4
		if timespent >= 4 then
			-- do stuff
		end
	end
end

InputEnded:

if input.UserInputType == Enum.UserInputType.MouseButton1 then
	breaking = false
end

use heartbeat increase your timer there. you can check breaking statement with every iteration. It is also
more performant against using tasks

For checking the variable changed you could just make a value somewhere and maybe use this:

coroutine.wrap(function()
while wait() do
[value].Value = variable
[value]:GetPropertyChangedSignal(“Value”):Connect(function()
–code
end)
end
end)()

My opinion is that it’s very simple and effective!

1 Like