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

how do I script a part to get destroyed once it hits another part?

Asked by
emrek 7
4 years ago

Ok so I just recently got into scripting and decided to try and make a tycoon game to challenge myself. There is 2 things I need help with now. So the first is when a part falls from the dropper on to the conveyor belt then hits the part at the end of the conveyor belt which would add money to the player. How would I make a script that gets destroyed once it hits the part at the end of the conveyor and gives money to the player.

My second question is how would I make it so that when a player CLICKS to buy a dropper they get money removed if they have enough money and the dropper starts working after that.

0
I cannot answer your second question but I was able to answer your first one. AzrefriskDreemurr 8 — 4y
0
the second question would be similiarto 50ShadesofLamps 5 — 4y

3 answers

Log in to vote
0
Answered by 4 years ago

Use a Touched event, these are fired whenever any physical objects touch with any part that the variable part is set to

The part variable is optional but there for the sake of easier readability

The hit inside of the function(hit) is the parameter, it basically gets what part touched us, you can read more about it on the dev hub here

The if hit.Name == "moneybrick" then line checks for the name of the brick that touched us, if the bricks name does not match the string it will not execute anything inside of this if statement

:Destroy() is a function of everything, removes its existence from the game (never tested it on services before, so idek if it can can destroy services lol)

local part = script.Parent
part.Touched:Connect(function(hit)
    if hit.Name == "moneybrick" then -- change the string value to the name of the money brick from the dropper
        hit:Destroy()
    end
end)
0
Bro, that's the same script as mine... AwesomeMrBird -79 — 4y
0
Yes but with more explanation and quality. LoganboyInCO 150 — 4y
0
I have a question, what if the object that is touching this part that has the script in it is a model? Because I want to make it so that when a model touches it, it removes it. WiggilyTooths 0 — 3y
Ad
Log in to vote
0
Answered by 4 years ago

Just simply put a normal Script inside the part you're trying to affect and paste this code:

script.Parent.Touched:Connect(function()
    script.Parent:Destroy()
end)
0
I have tried that and what happens is as soon as the part hits the conveyor belt it is destroyed, so I want a script that only makes it get destroyed when it hits the specific part. emrek 7 — 4y
Log in to vote
0
Answered by 4 years ago

Put this script in the part destroyer part.

function onTouch(part) 
    local yeet = part.Parent:FindFirstChild("PartName") --Change PartName to the name of the part that will be destroyed. Make sure the part that is going to be destroyed  is in Workspace. If not change part.Parent to Part.Parent.GroupName
    if (yeet ~= nil) then   -- if the part that will be destroyed exists, then
        yeet:Destroy()
    end 
end

script.Parent.Touched:connect(onTouch)

0
i copied that exactly and when I tested the game there was no errors but it just didn't work emrek 7 — 4y
0
Did you change part name to the part that is going to be destroyed? AwesomeMrBird -79 — 4y

Answer this question