How to replace terrain on mouse click

I am trying to make it so when a player clicks and if their mouse is on terrain it replaces it with ice

local movepart = game.Workspace:WaitForChild("HoverPart")
local mouse = game.Players.LocalPlayer:GetMouse()

local mousex = mouse.Hit.Position.X
local mousey = mouse.Hit.Position.Y
local mousez = mouse.Hit.Position.Z
local reigon = Region3.new(Vector3.new(mouse.Hit.Position.X,mousey,mousez), Vector3.new(mouse.Hit.Position.X,mousey,mousez))

function Mousedown()
	workspace.Terrain:ReplaceMaterial(reigon, 4, Enum.Material.Ground, Enum.Material.Ice)
end

mouse.Button1Down:Connect(function()
	workspace.Terrain:ReplaceMaterial(reigon, 4, Enum.Material.Ground, Enum.Material.Ice)
end)

I already have the part that goes to the mouse btw

2 Likes

how is this current script not working?

also i think you need to make a new region each time they click but im not a terrain expert

2 Likes

For starters, you need to update the region for the most recent mouse click. Currently your code only stores a region for the mouse’s initial hit (which is probably nil).

I believe you also probably want to call ExpandToGrid(4) on the region to make it match the terrain’s grid.

local resolution = 4 
mouse.Button1Down:Connect(function()
	if not mouse.Hit then
		return
	end

	local reigon = Region3.new(Vector3.new(mouse.Hit.Position.X,mousey,mousez), Vector3.new(mouse.Hit.Position.X,mousey,mousez)):ExpandToGrid(resolution)
	workspace.Terrain:ReplaceMaterial(reigon, 4, Enum.Material.Ground, Enum.Material.Ice)
end)
2 Likes

If it is a ball would you need grid for that
robloxapp-20250302-1226405.wmv (223.7 KB)

Video Of Gameplay Idk How To Make It Nonneeded download

this was it I have no idea why the other script did not work but thank you for your help!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.