How i can detect if a part is touching water terrain?

I’ve looking for this but as i see there’s not solution. If someone knows how to detect it please reply to this Topic.

1 Like

Try using raycasting, along with FindPartOnRay, because it takes into account parameters such as:


https://gyazo.com/42cc10b1aee567ae1e58b056a300be86
which in turn should return terrain, just set “terrainCellsAreCubes” as you wish and leave “ignoreWater” false.

2 Likes

Try using this code:

game.Workspace.Terrain.Appearance.Water:Touched:Connect(function(hit)
    print("Water touched!")

The code provided is not my own creation. All credit goes to: [SOLVED] How do I make a touched event for water? - Scripting Helpers

2 Likes

Did you read what was at that link or you just copied and pasted the script?

Sooo, What I am trying to do is If you touch the water something happens… I tried this script but didn’t work… and in the output it said: Appearance is not a valid member of Terrain

What is more, the code is incorrect, becuase it is missing ‘end)’.
Anyways it will not work even with ‘end)’.

2 Likes

It doesn’t work. image

Because he gave you something that does not work. There’s a trick to put an invisible block in the water and detect collisions with that block instead of water.

10 Likes

Thank you for pointing out what was wrong with the sample I provided; your invisible block solution is more effect.

1 Like

what if you have a map with water the size of arround 20k x 20k studs like the game im working on, it sure wont be affective and a waste of time

1 Like

going off of Kacpers suggestion , this is how you would execute it

make a LocalScript in StarterPlayer > StarterPlayerScripts
copy and paste the following code

-- Place this script in a LocalScript (such as in StarterPlayer > StarterPlayerScripts)

local player = game.Players.LocalPlayer
local inWater = false


--EDIT ONLY THE STUFF IN THIS BOX
------------------------------------------------------------

local waterPartPath = "water parts.Testpool"
local waterPart = workspace:WaitForChild("water parts"):WaitForChild("Testpool")


local function onHeadEnterWater()
	print("Head entered water!")
	-- Add your code for when the player's head enters water
end

local function onHeadExitWater()
	print("Head exited water!")
	-- Add your code for when the player's head exits water
end




------------------------------------------------------------

local function isHeadInWater()
	local character = player.Character
	if character then
		local head = character:FindFirstChild("Head")

		if head then
			local touchingParts = head:GetTouchingParts()

			for _, part in ipairs(touchingParts) do
				if part:IsA("Part") and part == waterPart then
					return true
				end
			end
		end
	end

	return false
end

local function handleWaterEvents()
	local currentlyInWater = isHeadInWater()

	if currentlyInWater and not inWater then
		inWater = true
		onHeadEnterWater()
	elseif not currentlyInWater and inWater then
		inWater = false
		onHeadExitWater()
	end
end

local function setupEvents()
	local head = player.Character:WaitForChild("Head")

	head.Touched:Connect(handleWaterEvents)
	head.TouchEnded:Connect(handleWaterEvents)
end

-- Setup events when the player's character is added
player.CharacterAdded:Connect(setupEvents)

-- Setup events in case the player's character is already present
if player.Character then
	setupEvents()
end

the script assumes you have a folder in workspace called “water parts” and the part you are referencing is called “Testpool”