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

How do I delete an object that is added to workspace after 5 seconds after killing a dummy?

What I want: I want a dummy that, when I kill it, it drops 3 tools that I can pick up. After 5 seconds, if the tools are not picked up, I want them to be deleted. I also want the dummy to regen after 5 seconds.

What I get: When I kill the dummy, it does not drop anything, it only regens after 5 seconds.

local dummy = script.Parent 
local dummy2 = dummy:clone() 
log = game.ReplicatedStorage.Tool


local humanoid = dummy:WaitForChild("Humanoid") 

humanoid.Died:connect(function() --Regen
	wait(5)
	dummy:Destroy()
	dummy2.Parent = workspace
	dummy2:MakeJoints()
	
	for i=1,3 do
		local clonedTool = log:Clone()
		clonedTool.Parent = workspace
		clonedTool.Position = dummy.Torso.Position
	end
end)

1 comment

0

You have many if humanoids then

2 answers

✓ Accepted answer

When using if statements in this context, you run the risk that the conditions won't pass. If the conditions don't pass, it means that the events inside of the if statements will never get connected.

You know that the Humanoid will exist, the only question is whether it exists now. Therefore, you can safely use WaitForChild to delay the script until it exists.

local humanoid = dummy:WaitForChild("Humanoid")

humanoid.Died:connect(function()
end)

--etc.

6 comments

0

I still don't quite understand, I started changing some stuff and ended up with the script I have now (script above is re-edited).

0

If you use FindFirstChild, you run the risk that the humanoid won't exist and that FindFirstChild will return nil. If it does return nil, that means "humanoid" equals nil. This will cause an error if you try to use it. Therefore, I suggest using WaitForChild on the humanoid and removing the if statements, so you can be 100% sure everything will get connected properly.

0

May you take a look at my updated code, I used WaitForChild and removed the if statements. Now, the dummies do regen after 5 seconds, but doesn't drop the 3 tools.

0

"dummy.Toroso.Position" You spelled Torso wrong. Make sure you're looking at the output, because it will tell you these things. I would also recommend combining the two functions, since they're listening to the same event. That isn't a functionality problem, though.

0

Check my code, please? :(

0

"clonedTool.Position = dummy.Torso.Position" This won't work because you already destroyed the dummy. Obviously, that means the Torso won't exist. Just destroy the dummy after you do everything concerning it. Again, make sure you look in the output, it tells you a lot.

working script below, except for the 3 item clones. i couldnt get it to work so i took it out but the cloning works.

good luck :)

local dummy = script.Parent 
local dummy2 = dummy:clone() 
local log = game.ReplicatedStorage.Tool
local clonedTool = log:Clone()

local humanoid = dummy:WaitForChild("Humanoid") 

humanoid.Died:connect(function() --Regen
	
		
	
        clonedTool.Parent = workspace
        clonedTool.Handle.Position = dummy.Torso.Position
wait(5)
    dummy:Destroy()

    dummy2.Parent = workspace
    dummy2:MakeJoints()
if clonedTool.Parent == workspace then
	clonedTool:Destroy()



end
end)

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