Procedurally generating water

Hello. I am trying to make procedurally generating water. This is my code currently.

local cf
local size

local s = 1750

function update()
	if game.Players.LocalPlayer.Character.Head.Position.Y <= 225 then

		if cf and size then
			workspace.Terrain:ReplaceMaterial(Region3.new(cf.Position-(size/Vector3.new(2,2,2)),cf.Position+size),25,Enum.Material.Water,Enum.Material.Air)


			if ((game.Players.LocalPlayer.Character.Head.Position*Vector3.new(1,0,1))-(cf.Position*Vector3.new(1,0,1))).Magnitude <= s/3.5 then
				return
			end
		end 

		--workspace.Terrain:FillCylinder(CFrame.new(game.Players.LocalPlayer.Character.Head.Position * Vector3.new(1,0,1)-Vector3.new(0,50,0)),160,s/2,Enum.Material.Water)
		
		

		cf = CFrame.new(game.Players.LocalPlayer.Character.Head.Position * Vector3.new(1,0,1)-Vector3.new(0,50,0))
		size = Vector3.new(Vector3.new(s,160,s))
		
		workspace.Terrain:ReplaceMaterial(Region3.new(cf.Position-(size/Vector3.new(2,2,2)),cf.Position+size),25,Enum.Material.Air,Enum.Material.Water)
	end
end

game.ReplicatedStorage.Events.Misc.WaterLoadRequest.OnClientEvent:Connect(update)

while true do
	wait(2)
	update()	
end

What it is supposed to do is generate a chunk of water terrain around the player. When the player gets close to the edge of that chunk, the game generates a new chunk of water around the player, but deletes the old one first. How can I do this?

1 Like