Should I use a raycast or a intangible npc character in front of the player to detect if water is near? If you can give me script where if water is near the player then use print("near water")

ANSWER:

local player = game.Players.LocalPlayer
local distance = 5 -- feel free to tweak this number
while true do
	local region = Region3.new(Vector3.new(player.Character.HumanoidRootPart.Position.X - distance, player.Character.HumanoidRootPart.Position.Y - distance, player.Character.HumanoidRootPart.Position.Z - distance), Vector3.new(player.Character.HumanoidRootPart.Position.X + distance, player.Character.HumanoidRootPart.Position.Y + distance, player.Character.HumanoidRootPart.Position.Z + distance))
	region = region:ExpandToGrid(4)
	local material, occupancy = game.Workspace.Terrain:ReadVoxels(region, 4)
	local size = material.Size
	for x = 1, size.X do
		for y = 1, size.Y do
			for z = 1, size.Z do
				if material[x][y][z] == Enum.Material.Water then
					print("Water")
				end
			end
		end
	end
	wait()
end

It's a rough mock-up but the final product will be more efficient and better

2 comments

1

Not sure exactly what the format of the returned arrays are so I can’t give an example, but you could use this: http://wiki.roblox.com/index.php?title=API:Class/Terrain/ReadVoxels

0

Works!