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

Why do my functions only work in the order they are written?

Trying to make a safezone for my game, using a region for the safezone and a region for the world itself. Whichever function I put first is the only one that runs. Can someone help me with this or give me a better method of doing it?

while JB do
	wait(0.5)
	
	local partsInJB = workspace:FindPartsInRegion3(jbRegion)
	for i, part in pairs(partsInJB) do
		if part.Parent:FindFirstChild("Humanoid") ~= nil then
			player = game.Players:GetPlayerFromCharacter(part.Parent)
			char = player.Character
			currentZone = "Jame's Bay"
			print(player.Name.." found in JB")
			
				newGui(player,currentZone)

			
		end
	end
	
end

while world do
	wait(0.5)
	
	local partsInWorld = workspace:FindPartsInRegion3(worldReg)
	for i, partw in pairs(partsInWorld) do
		if partw.Parent:FindFirstChild("Humanoid") ~= nil then
			player = game.Players:GetPlayerFromCharacter(partw.Parent)
			chawr = player.Character
			currentZone = "Leaving SafeZone..."
			print(player.Name.." is leaving the SZ")
			

				newGui(player,currentZone)

			
		end
	end
	
end

1 comment

0

Put the "while world do" and everything below it in a seperate script maybe? It's stuck on the first loop.

2 answers

✓ Accepted answer

Because any sort of loop is going to be ran before any code before it, with a while ... do loop, it will repeat that loop until it "breaks" or a value is set to false.

To keep it simple, you can use a coroutine function to run new threads in the same script.

This way, you can run 2 while loops without them disrupting eachother.

local JB = true
local world = true

local JBLoop = coroutine.wrap(function()
	while JB do
	wait(0.5)
	
	local partsInJB = workspace:FindPartsInRegion3(jbRegion)
	    for i, part in pairs(partsInJB) do
	        if part.Parent:FindFirstChild("Humanoid") ~= nil then
	            player = game.Players:GetPlayerFromCharacter(part.Parent)
	            char = player.Character
	            currentZone = "Jame's Bay"
	            print(player.Name.." found in JB")
	            newGui(player,currentZone)
	        end
	    end
	end
end)

local worldLoop = coroutine.wrap(function()
	while world do
	wait(0.5)
	
	    local partsInWorld = workspace:FindPartsInRegion3(worldReg)
	    for i, partw in pairs(partsInWorld) do
	        if partw.Parent:FindFirstChild("Humanoid") ~= nil then
	            player = game.Players:GetPlayerFromCharacter(partw.Parent)
	            chawr = player.Character
	            currentZone = "Leaving SafeZone..."
	            print(player.Name.." is leaving the SZ")
	                newGui(player,currentZone)
	        end
	    end
	end
end)

JBLoop()
worldLoop()

--// Everything will run without it being disturbed by the loops, coroutines are very useful!

print("Works!")

Hope this helped! Feel free to select this as an answer if this worked for you!

8 comments

0

I'll be sure to try it when I have the time

0

Alright!

0

Ok, so this method works, but I still have an issue with the safezone method itself. I have a part in the workspace for the safezone and for the world, but if you are in both at the same it glitches out. Is there a better way to do this?

0

Actually, I'll link a video as well. https://youtu.be/b_y2XRAAI7Y

0

You could do script.Parent.Touched and script.Parent.TouchEnded?

0

I've tried that but the only reason I'm using region3 is because touch and touchended is so broken. Like if you were to jump in the zone it would consider it as touch ended...

0

Also the newGui() function in the script uses wait() for the typewriter effect that might be the issue?

0

Might be, do some adjustments to your code to try to get it accurate as possible.

Try using two separate scripts, as it looks like your first loop is constantly going and therefore the script cannot proceed? Script 1

while JB do wait(0.5)

 local partsInJB = workspace:FindPartsInRegion3(jbRegion)
for i, part in pairs(partsInJB) do
if part.Parent:FindFirstChild("Humanoid") ~= nil then
	player = game.Players:GetPlayerFromCharacter(part.Parent)
	char = player.Character

	currentZone = "Jame's Bay"

	 print(player.Name.." found in JB")


	newGui(player,currentZone)

	end

end

`end`

(script 2 begin)

while world do

wait(0.5)
 local partsInWorld = workspace:FindPartsInRegion3(worldReg)
 for i, partw in pairs(partsInWorld) do
	if partw.Parent:FindFirstChild("Humanoid") ~= nil then
		player = game.Players:GetPlayerFromCharacter(partw.Parent)
 		chawr = player.Character
		currentZone = "Leaving SafeZone..."
		print(player.Name.." is leaving the SZ")
	 	newGui(player,currentZone)
	end

end

end

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