How do I make it so if a part touches another part something happens

I am trying to make a tycoon dropper and I confused how I am going to get the part that is being spawned in to destroy and give me a coin when it touches the End part

image
The red is the part spawner the green is where I want the parts to delete at and give me a Coin
After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local click = script.Parent
local partdrop = game.Workspace.PartDropper
local part
local End = game.Workspace.EndPart

click.MouseClick:Connect(function(plr)
	
	if click.MouseClick then
	    part = Instance.new("Part")
		part.Parent = partdrop
		part.Size = partdrop.Size
		part.Anchored = false
		part.Position = partdrop.Position + Vector3.new(0,-3,0)
	end
end)
End.Touched:Connect(function(hit)
	
	if End.Touched == part then
		
		plr.leaderstats.Coins.Value +=1
	end
	
end)
-- This is an example Lua code block
3 Likes

The code you have will not work since the part will always be changing each time you click (meaning the most recent block that drops will be able to interact with the end part and nothing else). What you should do is attach a Touched event to the part you made where it checks to see if it touches the end zone. If so, give money and destroy the part.

1 Like

Can you show me in code what this would look like

Below this code, you can do the following:

part:Touched(function()
-- your other codes in here
end)

also im pretty sure plr.leaderstats.Coins.Value +=1 wont work as the plr in the mouseclick event is in another scope.

End.Touched:Connect(function(hit)

if End.Touched == part then
	
	plr.leaderstats.Coins.Value +=1
end

end)
this wont work too pretty sure because ur doing
if End.Touched == part then
end
instead do: if hit.Name == “part” then

1 Like

What would I put after this when the part is touched

What would I put in this part of the script

Well, you just add what you have mentioned on top. You mentioned that you want to destroy the part and give a coin. Make one line that destroys the part and one line that add a coin to the player.

well,

local click = script.Parent
local partdrop = game.Workspace.PartDropper
local part
local End = game.Workspace.EndPart

local plr

click.MouseClick:Connect(function(clr)
	
	plr = clr
	if click.MouseClick then
		part = Instance.new("Part")
		part.Parent = partdrop
		part.Size = partdrop.Size
		part.Anchored = false
		part.Position = partdrop.Position + Vector3.new(0,-3,0)
	end
end)
End.Touched:Connect(function(hit)

	if hit.Name == "Part" then

		plr.leaderstats.Coins.Value +=1
	end

end)

this might not work but try.
Not to be rude or anything, did u script this or did ai do it for you? Again, just asking

1 Like

The script does not know what plr is so what do I need to do I already tried putting the End.Touched function inside the click.MouseClick function.

cant help you if you dont cooperate, show me the output

image

I first of all am trying to cooperate this is why I made the post and second here are the stuff you asked for. (I made this code myself)

did you copy my code in the first place? i defined plr in my code

No I did not see that my bad I will change it

so?, did it work tell if it works or not

This code works but I want the part to destroy and give me 1 Coin not 7

then script it? Thats really easy so you will be to figure that one out really easily