Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

2 comments

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.

1

Tysm!!!!

1 answer

✓ Accepted answer

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

1 comment

0

Thank you!

This discussion is preserved as it was. New answers, comments, and votes are closed.