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

How Would I Loop A Whole Script, Not Just One Section?

Asked by 3 years ago

I've been looking and I can't find a way to be able to loop a whole script. This is because I am trying to keep looping a check that sees if a player has something in their inventory. Instead, if I were to but a while loop into the section it only loops the check but I want it to loop the whole script because there is a gun script under it (this is the "this gun for fps" assault rifle script.) Because it is only looping the check for the players inventory the gun does not function. Please help.

Thank you!

(script)

while true do --Supposed to loop the whole script but it only loops until the end.
    wait()
if game.Players.LocalPlayer.Backpack:FindFirstChild("MinionWeaponSpeed") then
    FireRate = 3.75/30
else
        FireRate = 5/30
    end
end
--------------------- TEMPLATE ASSAULT RIFLE WEAPON ---------------------------
-- Waits for the child of the specified parent
local function WaitForChild(parent, childName)
    while not parent:FindFirstChild(childName) do parent.ChildAdded:wait() end
    return parent[childName]
end
1
Instead of doing the loop, I think you could just do game.Players.LocalPlayer.Backpack.ChildAdded:Connect(function(child) [CODE] end). It will run whenever a tool is added to the player's backpack. BunnyFilms1 297 — 3y
1
Tysm!!!! MrSarp_shoot 37 — 3y

1 answer

Log in to vote
0
Answered by
0hsa 193
3 years ago

ok, know you found a solution but for future use: you can do that with coroutines

coroutine.wrap(function()
    while true do
        `code`
    end
end)() -- <-- dont forget this or it won't run

also:

local loop = coroutine.wrap(function()
    while true do
        `code`
    end
end)

-- somewhere else in the script when you want to do the loop
loop()

it will run in a different thread so the script can do other things while that loop is happening

0
Thank you! MrSarp_shoot 37 — 3y
Ad

Answer this question