Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How can I delete a part and increase a value when it touches another part?

I am working on a tycoon from scratch. This is the script for the collector part at the end of the conveyor belt.

Script: by metryy who suggested it below

Part = script.Parent
Value = script.Parent.Value

Part.OnTouched:Connect(function(hit)
    if hit.Name == "Cash" then
        hit:Destroy()
        Value = Value + 1
    end
end)

In the output, it comes up with:

OnTouched is not a valid member of Part

I need a fix to this script. Thanks

4 comments

0

Sorry scriptinghelpers is not a request site, https://scriptinghelpers.org/help/community-guidelines. Please narrow the question to a single problem or post your own attempt.

0

You could make your life easier by grabbing a free tycoon kit from the toolbox and editing it from there, but if you insist on doing one from scratch, you may also want to learn Lua from scratch, https://developer.roblox.com/learn-roblox/coding-scripts. Good luck!

0

If you're new, I'd definitely recommend and advise that you use a Tycoon Kit, preferably Zed's Tycoon Kit, but you'd probably need at least 1+ years of experience to make a Tycoon from scratch.

0

This is not a request site.

2 answers

This is the page of the wiki you want: https://developer.roblox.com/api-reference/event/BasePart/TouchEnded

This is the sample code on that page

    local part = script.Parent
     
    -- Create a BillboardGui
    local bbgui = Instance.new("BillboardGui", part)
    bbgui.Size = UDim2.new(0, 200, 0, 50)
    bbgui.Adornee = part
    bbgui.AlwaysOnTop = true
    local tl = Instance.new("TextLabel", bbgui)
    tl.Size = UDim2.new(1, 0, 1, 0)
    tl.BackgroundTransparency = 1
     
    local numTouchingParts = 0
     
    local function onTouch(part)
    	print("Touch started: " .. part.Name)
    	numTouchingParts = numTouchingParts + 1
    	tl.Text = numTouchingParts
    end
     
    local function onTouchEnded(part)
    	print("Touch ended: " .. part.Name)
    	numTouchingParts = numTouchingParts - 1
    	tl.Text = numTouchingParts
    end
     
    part.Touched:Connect(onTouch)
    part.TouchEnded:Connect(onTouchEnded)

You can make what you want pretty easily from the sample code.

1 comment

0

Instance.new("class", parent) is deprecated. use local obj = Instance.new("class") obj.Parent = parent

There's a "onTouched" event for every part in Roblox that fires when another part touches it. You can connect a function to it that deletes the part you want (with the Destroy func) and increases whatever value you want by 1 with the + plus operator.

Example:

Part.OnTouched:Connect(function(hit)
	if hit.Name = "DroppedPart" then
		hit:Destroy()
		Value = Value + 1
	end
end)

Hope this helps. :)

1 comment

0

Please refer to updated desc above.

This discussion is preserved as it was. New answers, comments, and votes are closed.