Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Wait in for i,v loops makes delays how do I counter this? [SOLVED]

Asked by
Creacoz 210 Moderation Voter
5 years ago
Edited 5 years ago

So what I'm trying to do is I'm trying to remove everyone's sword in their inventory but making it reappear after like 2 seconds but when I put it in the loop, it makes it delayed.

this is not the full script because it would be too long but thats what im trying to do

   if spawnz and player.TeamColor == BrickColor.new("Fossil") then

torso.CFrame = CFrame.new(spawnz.Position + Vector3.new(0, 2, 0))

player.Character.Humanoid:UnequipTools()

player.Backpack:ClearAllChildren()

wait(2)

local sword = game.ReplicatedStorage.Weapons.Sword

sword:Clone().Parent = v.Backpack

end

how would I make it so after 2 seconds after everyone is done being teleported they get back their swords?

0
wdym delayed? Like it takes longer than 2 seconds? YTRaulByte 389 — 5y
0
it does it after 2 seconds for everyone Creacoz 210 — 5y
0
for example : theres 2 players, one will lose their sword then get it back before the second one will even lose their swords Creacoz 210 — 5y
0
you can make two separate i v in pairs loops with a wait in between, one to take it away and to teleport, and the other to give back sword Fad99 286 — 5y
View all comments (2 more)
0
Oh yeah thats intelligent Creacoz 210 — 5y
0
Fad99. That way, there might be some performance issues as you're recursing loops twice that both access the swords. renzocedricpura 47 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

Use the spawn() function. Before we get to the point, let me explain little bits of information so you can understand how to use it.

What is a task scheduler?

A task scheduler is essentially a large loop that goes through all of the processes it has to do and executes them. It runs as fast as it possibly can, and in Lua this is a number pretty close to 0.03.

What are threads?

A thread is a group of computer instructions that can be executed by a task scheduler, in particular Lua's thread task scheduler. In simple words, it is a block of code that runs in serial. Multithreading is the ability to send multiple threads out to the scheduler and have it execute them simultaneously. It is an important part of parallel computing. When you create a new thread, that thread will seem to run at the same time as the rest of the code. This can be particularly useful for when you want to run processes that may take a long time, or perhaps even infinite time, side by side, such as important game loops.

Using spawn

What spawn()does is wait for the current task scheduler update to end. Then, it puts in your request to make a new thread with some code content in it into the task scheduler and will wait until the next task scheduler update. When that update happens, the thread will execute and your code will begin executing. The content of the thread itself is the function you supply to spawn().

void spawn ( function callback )

It runs the specified callback function in a separate thread, without yielding the current thread.
The function will be executed the next time Roblox’s Task Scheduler runs an update cycle. This delay will take at least 29 milliseconds but can arbitrarily take longer, depending on the target framerate and various throttling conditions. spawn() does not return such a reference_. It's a much more light-weight, RBX.Lua native function construct that simply queues up a new thread to the task scheduler. Once you create it, you cannot get a reference to it and you simply wait until it runs to completion.

How do I apply it to my script?

Put a spawn() function at the start of your code. Then, create a new anonymous callback function at the first argument of the function. After that, move your code inside the callback function.

spawn(function()

    if spawnz and player.TeamColor == BrickColor.new("Fossil") then

    torso.CFrame = CFrame.new(spawnz.Position + Vector3.new(0, 2, 0))

    player.Character.Humanoid:UnequipTools()

    player.Backpack:ClearAllChildren()

    wait(2)

    local sword = game.ReplicatedStorage.Weapons.Sword

    sword:Clone().Parent = v.Backpack

    end

end)

And your code is done! No more delays since you have applied spawn() to it.

0
Woah thank you so much! You really took your time on all of this I'm so happy. Creacoz 210 — 5y
Ad

Answer this question