How do I connect a touched event to a terrain change script?

I am creating a mining game and have spent the past few days making assets, the assets I have are, a shovel and an excavator with working joints but I can’t connect the touched function the the bucket for the life of me, The terrain filling script someone gave me a hand with and wont respond to me so I can’t understand how to get the bucket’s position instead of the mouse.

function dig(player,blob)
     workspace.Terrain:FillBlock(CFrame.new(blob),Vector3.new(5,5,5),--[[mouse.Target.Material]]Enum.Material.Air)
end
game.ReplicatedStorage.Dig.OnServerEvent:Connect(dig)

And as the comment says mouse.Target.Material so I tried calling the bucket mouse and that didn’t work, the touched event is fairly simple, its just the connecting i’m struggling with, here is the function i’m using in a script in the bucket

Where I Am At Trying;

local terrain = game.Workspace.Terrain
terrain.Touched:Connect(function(dig)
	if dig.Name == "Bucket" then
		workspace.Terrain:FillBlock(CFrame.new(blob),Vector3.new(5,5,5),--[[mouse.Target.Material]]Enum.Material.Air)
	end
end)

Normally scripting can be a whizz with functions for me but this time I got nothing

Are any errors printed out when the code is run? I also noticed blob is not defined.

Yeah there is it is

11:47:51.990 - Workspace.Excavator.Bucket.Script:4: invalid argument #1 to ‘new’ (Vector3 expected, got nil)

on line 4 it is something to do with the blob, I just extracted the line from the shovel I have, it uses a local script and a script

the local script;

script.Parent.Equipped:connect(function(mouse)
	script.Parent.Activated:connect(function()
		local char = script.Parent.Parent
		if script.Parent.Value.Value == false then
			game.ReplicatedStorage.Dig:FireServer(mouse.hit.p)
		end
		if script.Parent.Value.Value == true then
			game.ReplicatedStorage.Place:FireServer(mouse.hit.p)
		end
		--workspace.Terrain:FillBlock(mouse.hit,Vector3.new(7,10,7),--[[mouse.Target.Material]]Enum.Material.Air)
	end)
end)

and the script;

local value = script.Parent.Value
function dig(player,blob)
	if value.Value == false then
		workspace.Terrain:FillBlock(CFrame.new(blob),Vector3.new(5,5,5),--[[mouse.Target.Material]]Enum.Material.Air)
		value.Value = true
	end
end
game.ReplicatedStorage.Dig.OnServerEvent:Connect(dig)
local value = script.Parent.Value

function fill(player,blob)
	if value.Value == true then
		workspace.Terrain:FillBlock(CFrame.new(blob),Vector3.new(2,2,2),--[[mouse.Target.Material]]Enum.Material.Ground)
		value.Value = false
	end
end
game.ReplicatedStorage.Place.OnServerEvent:Connect(fill)

I should probably have put these in the post but these do not create errors and I can’t see and explicit local blob = blank

You should just pass mouse.hit instead of mouse.hit.p because mouse.hit is already a cframe value, so you can just write blob instead of CFrame.new(blob)

on the shovel scripts? or on the excavator script, just thinking weather it would be worth testing it on the excavator

Try changing all instances of mouse.hit.p to mouse.hit in the local script.

okay I will do.
30 characterscharacters

I now get this error with the shovel
image

now change the CFrame.new(blob) to blob

that works now where do I go from here?

What is the script supposed to be doing but isn’t already doing?

its not the shovel, the shovel works perfectly fine now but its the excavator. What I am trying to do is make it so when the bucket collides with the ground the terrain beneath it is destroyed but I cant get the terrain to destroy because I believe the script is wrong as it returns

12:06:59.272 - Argument 1 missing or nil

on line 4 of the script

local terrain = game.Workspace.Terrain
terrain.Touched:Connect(function(dig, blob)
	if dig.Name == "Bucket" then
		workspace.Terrain:FillBlock(blob,Vector3.new(5,5,5),--[[mouse.Target.Material]]Enum.Material.Air)
	end
end)

but that line was made for a shovel and where a player clicks, not where a part is which is where I am stumped

the game is here; Terrain Destroy - Roblox
If you are trying to visualize what I am trying to do

I don’t think the terrain has a Touched function, I think you would have to find out when the bucket hits the terrain, using something like this:

local terrain = game.Workspace.Terrain
Bucket.Touched:Connect(function(hit)
    local bucketCFrame = Bucket.CFrame
    workspace.Terrain:FillBlock(bucketCFrame, Vector3.new(5,5,5), Enum.Material.Air)
end)

So I added a variable to the bucket and it gives no errors but it still doesn’t destroy the terrain, could it be that it needs to do the math of below the bucket instead of its location?

by that I mean I created a variable above the script to reference the bucket

Maybe turn the cancollide of the bucket off when it is in use, so it can burrow into the ground.

1 Like

That works wonders thanks so much!