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

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

Asked by 3 years ago

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
0
Put the "while world do" and everything below it in a seperate script maybe? It's stuck on the first loop. TheMiniWintress 7 — 3y

2 answers

Log in to vote
2
Answered by 3 years ago
Edited 3 years ago

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!

0
I'll be sure to try it when I have the time DejaSketch 84 — 3y
0
Alright! killerbrenden 1537 — 3y
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? DejaSketch 84 — 3y
0
Actually, I'll link a video as well. https://youtu.be/b_y2XRAAI7Y DejaSketch 84 — 3y
View all comments (4 more)
0
You could do script.Parent.Touched and script.Parent.TouchEnded? killerbrenden 1537 — 3y
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... DejaSketch 84 — 3y
0
Also the newGui() function in the script uses wait() for the typewriter effect that might be the issue? DejaSketch 84 — 3y
0
Might be, do some adjustments to your code to try to get it accurate as possible. killerbrenden 1537 — 3y
Ad
Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

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

Answer this question