How to delay something?

Hey so i’m looking for a way to delay something
I tried delay however due to the variable changing it did not work.

Here’s an example of what I tried with delay:

local x = 5 
delay(2, function() 
print(x * 2)
end)

x = 12
| output: 24

What I’m looking to do is have it print 10, the original without pausing the entire script

Anyone have any ideas? Thank you!

You could use Wait(amount of time goes here) but I don’t think that will solve your problem.

You should try to find a tutorial/develop forum for it if you did already please check back on it so you can auto correct yourself

Hmm what do you mean by that? Wouldn’t this be seen as a develop forum?

Well yes I am saying where did you find this script from, I am telling you to go back where you got the script…

It’s a really simple answer, most beginners would already know this.

You should use the wait function.

I made the script lmao as an example here, so it’s understood.

The script can’t pause, it needs to continue on.

How would you even know how to create a script without looking an develop forum or tutorial unless you just put random script

Then you could run it on a separate thread via spawn or a coroutine which will allow it to not interfere with the current thread and pause the script.

preserve the value of x before it is set again.

local x = 5
local y = x

delay(2, function()
    print(y * 2)
end)

x = 12

Not sure why you are doing this. Also I suggest using coroutine or or something else instead of delay as suggested by @Interaxis.

As the script continues the run, the Y will be overwritten (as x will change on the next run)

You can preserve the value inside of the function so it won’t be changed if you change the variable.

It shouldn’t change though either way.

?? The code outputs 10.

image

what are you trying to do. I’ve never had a reason to use delay or spawn over years of using lua on roblox.

It does not output 10 because the function will lookup x after 2 seconds.

local x = 5
delay(2, function()
    local y = x
    print(y * 2)
end)

x = 12

will output 24
image

2 Likes

Yes apologises, thank you very much! Marked as solution.