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
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.
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.
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
Do what the others are suggesting. The logic behind your check function should go like this:
The swimming variable defaults to false.
Loop through each region.
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.
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.
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.