Waiting a short period of time to see if a value changes

i have 2 values, one that is a number value and changes like this :

local function function1()
if value1 == 1 then
-- the code
value1 = 2
else if value1 ==  2 then
--the code
value1 = 1
end
end

the 2nd value is a bool value and is set to true when the function is triggered, then to false when the function is finished :

local function function1()
if value1 == 1 then
value2 = true
-- the code
value1 = 2
value2 = false
else if value1 ==  2 then
value2 = true
--the code
value1 = 1
value2 = false
end
end

i would like to make the code wait for a short period of time, around 2s then see if the function was triggered during those 2 seconds, if the function was not called then the 1st value will be set to 1.

i cannot simply add a wait() because during the wait, the function could have been triggered again and the code wont be able to check. in other words if the function was spammed, the 1st value would be switching between 1 and 2 in a weird way and thats because it changes after the wait, and also changes when the function is triggered

it took me really long to make this as clear as possible, so i hope its clear enough.
thanks a lot

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

-- This is an example Lua code block

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

One question is how, and when are you executing these functions which is causing the problematic behavior? A .Changed event perhaps?

For this try considering a debounce technique, this technique solves the problem of what happens during a wait as it sets a signal (sets a variable to true) before the wait happens to tell the script that function has executed already.

the function executes when you press LMB

If Value1 is stored in lets say a NumberValue or IntValue in a model then you can use .Changed to detect a change in it’s value.

for Example

Local Value1 = Script.Parent.Value1
Value1.Changed:Connect(function()
– Your code here
end)

i want to do the opposite of that, see if it has not changed

This is unnecessary. You don’t need to check if the value was changed. If the value was untouched then there is really no need to detect that.

If you are making a fighting system I suggest using tick and UIS. You can also do some metatable magic, but that is rather complex.

There are also other ways to detect changes. I have a strong feeling you are overthinking this problem and making it much more complex than it needs to be.

i am making a combat system, i dont see how i could use tick(), it only records how much time has passed, not events.
what im trying to do is see if the player attacked again during those 2 seconds, if they did not attack, then i want to print something.

sorry if i replied late i was learning about tick() and experimenting with it.

You would be better off detecting the time between hits in a loop. Then if its past 2 seconds then just reset the counter.

i was looking that up, but i didnt find how to reset tick(), could you please show me how to do that?

local RS = game:GetService("RunService")

local Tick = tick
local HitTick = Tick() + 2

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()

Mouse.Button1Down:Connect(function()
    HitTick = Tick() + 2
    print("Pow")
end)


RS.RenderStepped:Connect(function()
    if Tick() >= HitTick then
        print("Did not attack")
        HitTick = Tick() + 2
    end
end)
local HitTick = Tick() + 2

prints an error
“attempt to call a number value”

edit : i removed the ()

but i got another error since im using a server script, RunService can only be used from a local script

Please send me all your code. Programmer | Cliffon#2633

added you Eiji From Jumia#9999

You are making a combo counter that resets over time? I have already done that try checking it out:

1 Like

I managed to get it to work with something like this:

local prevTime = 0
local currTime = 0

prevTime = currTime
currTime = tick()
local passedTime = currTime - prevTime
if passedTime >= 2 then
	Value.Value = 1
end

thank you though!

2 Likes