Region3 problem

So I was making a system, so you can swim inside certain parts like in Flood Escape 2, and I made a working system.

It worked wonderfully with 1 part, however when theres 2 water parts, if you swim inside one, the Swimming value gets changed to true, but gets changed back to false because you are not in the other region.

Basiclly:
You are swimming in Water1, so Swimming = true
However you are not swimming in Water2, so Swimming = false

How would I prevent this from happening?

Use two different variables? For example Water1 would toggle the bool for Swimming1 and Water2 would toggle the bool for Swimming2? Not too sure on what you are asking though. If you still wanted to find out if the player was in water, you could have another variable called Swim. Just use different variables; but again I may have misunderstood sorry.

You can loop through all of the swimming regions, and if the player is just within one, then set swimming to true.

Psuedocode:

swimming = false
for _, water in pairs(waterParts) do
      if (player in region) then
            swimming = true
            break
      end
end

Thats what I did, but swimming gets set back to false because the player is not in the other region

I imagine your code looks something like

for all water parts do
     if (player in region) do
          swimming = true
     else
          swimming = false
     end
end

which is different than what I have. You shouldn’t be setting swimming to true/false for every single region. All you need to do is check to see if the player is inside of 1 of those regions. If they are, then you can set swimming to true. If the player isn’t inside of any regions, then you can set swimming to false.

I dont really understand, yes that is in short what my code is, but the code you showed before doesnt set swimming back to false

How are you consistently checking whether or not the player is in a water region? Are you using a loop?

You can store booleans in a table and then check all of them

local SwimmingTable = {} 
local IsSwimming 


function Check()
    local SwimCheck = false

    for i,v in pairs(SwimmingTable) do
        if v == true then
            SwimCheck = true
        end
    end

    return SwimCheck
end

for i,v in pairs(Regions) do
    if condition then
        SwimmingTable[v] = true

        IsSwimming = Check()

        --Do whatever you need
    else
        SwimmingTable[v] = false

         IsSwimming = Check()

        --Do whatever you need
    end
end
2 Likes

Yes, I use a while loop to check it

I see. Im assuming condition is the check if the player is in region3?

Do what the others are suggesting. The logic behind your check function should go like this:

  1. The swimming variable defaults to false.
  2. Loop through each region.
  3. If the player is swimming in that region, set the swimming variable to true and break the loop. We don’t need to check the other regions because we know that the player is in at least one of them.

If the player isn’t in any of the regions, the variable will just remain false, which is what we want.

Run this function in your loop to consistently check the player’s swimming status.

Hope this helps!

Im still confused, if the loop breaks then the swimming value wont get set back to false when you jump out of the water

No , it doesn’t matter whether you break the loop or not , it’s simply a check for whether the player is present in any of the specified regions. When you jump back out the function will be triggered again anyway.

1 Like

OHHH I see now! Break the loop when it detects the player in any region every single time the loop runs!

Break the check loop I mentioned. You’re using two loops; one inside of another.

local IsSwimming = false

while true do
    local InRegion = false -- Defaults to false
    for _, Region in pairs(RegionList) do
        if PlayerInRegion then -- Player is swimming in a region
            InRegion = true
            break -- Only breaks the "for" loop
        end
    end
    IsSwimming = InRegion

    wait()
end

IsSwimming will always reflect the correct value here. We use the separate variable InRegion just to take advantage of this method; if we used IsSwimming directly, it would get switched to false constantly and wouldn’t work.

It isn’t required, but it should still be done for efficiency’s sake. No reason to keep going if you get a positive.

1 Like