Problems with WaterColor and WaterTransparency script

I am trying to write a script that changes the WaterColor and Watertransparency properties when the player is inside a certain zone determined by a part. Sadly, the only script I found was not working for me and it kept switching between the two states of the water.

The script that I made runs through the loop infinitely fast and laggs out the game. Also, the water doesn’t stay always the same color and transparency even though the character is already inside the part, and it mostly happends when it prints “character wasn’t found inside the part” even though he is.

I had found a script that had a similar purpose, at WaterColor local script not working? , but it did the same “switching between two states randomly” thing for me, so I thought the localization method was not precise, which is why i tried to make my own.

here’s my script (i’m fairly new to it so I don’t understand what most of it means):

local RegionPart = game.Workspace.RegionPart
local pos1 = RegionPart.Position - (RegionPart.Size / 2)
local pos2 = RegionPart.Position + (RegionPart.size / 2)
local region = Region3.new(pos1, pos2)

while true do
	wait (0.5)
	local partsInRegion = workspace:FindPartsInRegion3(region, nil, 1000)
	for i, part in pairs(partsInRegion) do
		if part.Parent:FindFirstChild("Humanoid") ~= nil then
			print("Player Found in the region!: ".. part.Parent.Name)
			game.Workspace.Terrain.WaterColor = Color3.fromRGB(127,0,0)
			game.Workspace.Terrain.WaterTransparency = 0
		end
		if part.Parent:FindFirstChild("Humanoid") == nil then
			print("player wasn't found in the region")
			game.Workspace.Terrain.WaterColor = Color3.fromRGB(0,0,127)
			game.Workspace.Terrain.WaterTransparency = 1
		end
	end
end

Well, you loop through all the parts inside the region and check if they’re a sibling of a Humanoid. If it is, you make the water red, else you make it clear blue.

Accessory parts are not siblings of humanoids and thus, as you loop through these parts, it will continuously switch color / transparency more or less at random. The visible result solely depends on the last part in the list, for which you cannot predict what it is going to be.

If you want to make the water red if any Humanoid sibling is found (and keep it clear blue elsewise), consider something like this instead:

local found -- init local variable with nil
for i, part in pairs(partsInRegion) do
    if part.Parent:FindFirstChild("Humanoid") then -- ~= nil is not required, although you can keep it if you prefer
        found = true -- found a humanoid sibling, set found to true
        break -- immediately leave the loop, we do not need to check any more parts
    end
end
if found then
    -- there's a Humanoid sibling
    -- make the water red here
else
    -- there's no Humanoid sibling
    -- make the water clear blue here
end
2 Likes

I would like to recommend you also switch over to using Workspace | Documentation - Roblox Creator Hub and making a table of each players character, this will help with performance later down the line if you add more onto your map as it will only search for characters and ignore everything else, what is most likely happening is that the bottom portion of your code is going off from hitting the baseplate and telling the script that “THERES NO HUMANOID CHANGE IT BACK QUICKLY”, my suggestion may help fix that issue a bit more as well, apply it to the top fix by flubb as well and it’ll go even better.

2 Likes

when I copy paste the modifications you made (thank you very much for helping!), it underlines the "part " written right before "in pairs(PartsInRegion). do you know what that means? Also, im not sure where I am supposed to write the changes to the water properties i’m kinda lost :sweat:
it says in the output: [18:17:56.477 - ServerScriptService.Script:10: Expected ‘in’ when parsing for loop, got ‘part’]

Pardon, I had a typo. I’ve updated my post above to correct it and point out the proper locations for the water changing lines.

1 Like

Aw man, now that I replace everything I made with yours (but keep the water properties and the four original variables on line 1-4) it underlines partInRegion as a “unknown global” and as I understand it it is a variable that wasn’t defined? So that means I should keep my local partsInRegion = workspace:FindPartsInRegion3(region, nil, 1000)
? but if I do that there is just nothing that happens.

also, in my print when the water properties change when the player is inside, the part is also an unknown global.

I’m currently trying to learn scripting but this is too complicated for me sry for asking too much if I do. :confused: