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

Destroying player objects?

Asked by 8 years ago

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

Log in to vote
3
Answered by 8 years ago

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

Ad
Log in to vote
1
Answered by 8 years ago

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"

0
Mine is better lel. TheDeadlyPanther 2460 — 8y
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! rexbit 707 — 8y

Answer this question