Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

CLOSED: How would I detect if smooth terrain water is near the player?

Asked by
Griffi0n 315 Moderation Voter
6 years ago
Edited 6 years ago

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

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 mattscy 3725 — 6y
0
Works! Griffi0n 315 — 6y

Answer this question