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

Destroying player objects?

I made a script where if the player drops their hat/s this script is suppose to destroy them. My only issue is how would I define it in the workspace so when the hat object appears in workspace its destroyed.

local Hat = Instance.new ("Hat")
if game.Workspace.Hat == true then
	game.Workspace.Hat:Destroy()
end

Its an issue with the "Hat" and I don't know how I would define a hat object

2 answers

✓ Accepted answer

Do this:

game.Workspace.ChildAdded:connect(function(obj)
	if obj:IsA("Hat") then
		obj:Destroy()
	end
end)

Instance.new is used for creating objects, not waiting to see if one exists.

Hope I helped ;P

~TDP

So the best way of doing this is making a listener that fires and event when a child is added to the workspace. This will be done by doing the ChildAdded Instance. This will make a non lagging, efficient code. This can be done like so:

game.Workspace.ChildAdded:connect(function(newObj)
	if newObj:IsA("Hat") then 
	newObj:Destroy()
	end
end)

This will destroy any object that enters workspace with a ClassName of "Hat"

2 comments

0

Mine is better lel.

0

Answers, Answers! Questions, Questions! REPUTATIONS, REPUTATIONS! Your script can be chopped down by using a ChildAdded event, second What is that for in there,and finally MY GAME IS LAGGY!

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