How could I detect when a noob touches this part, and when it does, destroy it?

I want this script to detect when a noob from the Noob Printer 1 touches this brick, and when it does, I want it to destroy.

I am not very good at scripting, and this is a tycoon, so the noobs are supposed to be destroyed when they hit the end. The noobs just end up stacking on the brick and don’t destroy.

As I said, I am not very good at scripting, so I haven’t done much. I have tried to look on the dev hub but haven’t found anything.
This is what I have so far:

function bye()
script.Parent.Touched:connect(function(hit)
	local noob = game.Workspace["Noob Printer 1"][" "]
	if hit.Name == noob  then noob:Destroy()
  --script to open the door
end
	end)
	end

while true do
	wait(0.2)
	bye()
	end

(The [" "] is the name of the noob I’d like to destroy
Any help would be appreciated.

6 Likes

You forgot to add,

if hit.Name == noob.Name then noob:Destroy()

Also please organize your code more, the end placements are KILLING me.

3 Likes

lol sorry but it still didn’t work, the noobs just stay on the brick and don’t destroy

4 Likes

Why are you using a while loop to run your function? You can just do,

script.Parent.Touched:Connect(function()

end)

and it’ll still run as long as you put it before the while loop.

3 Likes

So you’d like me to just remove the while loop?

3 Likes

Try that, ok yes ok yes ok yes, sorry 30.

3 Likes

I tried it

function bye()
script.Parent.Touched:Connect(function(hit)
	local noob = game.Workspace["Noob Printer 1"][" "]
	if hit.Name == noob.Name  then noob:Destroy()
		end
	end)
end

and nothing happened

Remove the bye function and see if that works.

script.Parent.Touched:Connect(function(itemHit)
	local Character
	if itemHit.Parent:FindFirstChild("HumanoidRootPart") then
		Character = itemHit.Parent
	elseif itemHit.Name == "HumanoidRootPart" then
		Character = itemHit
	end --You can remove line 3 to 7 if you don't want it to check for a humanoid.. then remove the "Character and" from the if statement below.
	if Character and Character.Name == "Noob" then script.Parent:Destroy() end
end)

You’d have to put parameters to determine what a “noob” is for you. I used the Character’s Model Name being Noob as the parameter.

You also don’t need a while loop for this as the script will activate, whenever it’s touched by any object.

2 Likes

here’s without checking for HumanoidRootPart
@Just2Terrify already made a comment for it

--You can remove line 3 to 7 if you don't want it to check for a humanoid.. then remove the "Character and" from the if statement below.
script.Parent.Touched:connect(function(hit)
  if hit.Parent.Name == "Noob" then 
    hit.Parent:Destroy()
  end
end)
2 Likes

Thank you very much! This worked when I didn’t search for the HumanoidRootPart, and just the name of the noob, though i still get the output error, Workspace.killbrick.Script:2: attempt to index nil with ‘Name’

1 Like

This script worked but I still gave the solution to @Just2Terrify
thanks though

2 Likes

Hey quick question,
do you know how to have this script run without the error,

Workspace.Base1.Friend Dropper.killbrick.Script:2: attempt to index nil with ‘Name’ - Server - Script:2
23:08:50.108 Stack Begin - Studio
23:08:50.108 Script ‘Workspace.Base1.Friend Dropper.killbrick.Script’, Line 2 - Studio - Script:2
23:08:50.108 Stack End - Studio

I’m unsure as to why it’s causing an error, as I don’t have an error on my end; respond with your script.

I apoligize for the late response, this is my code:

script.Parent.Touched:Connect(function(hit)
	if hit.Parent.Name == " " then 
		hit.Parent:Destroy()
	end
end)

“ “ is the name of the noobs that i need it to destroy
And here is the error message again:

Workspace.Base1.Friend Dropper.killbrick.Script:2: attempt to index nil with ‘Name’ - Server - Script:2
23:08:50.108 Stack Begin - Studio
23:08:50.108 Script ‘Workspace.Base1.Friend Dropper.killbrick.Script’, Line 2 - Studio - Script:2
23:08:50.108 Stack End - Studio

1 Like

The simple fix to this is to check if hit.Parent ~= nil…

script.Parent.Touched:Connect(function(hit)
	if hit ~= nil and hit.Parent ~= nil and hit.Parent.Name == " " then 
		hit.Parent:Destroy()
	end
end)

alright thanks i am on vacation though so i will try it when i get back and let you know how it goes

1 Like

alr thanks this worked

eeeeeeeeeeee

1 Like

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