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

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

Asked by 7 years ago
Edited 7 years ago

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)

0
You have many if humanoids then DaCrazyDev 444 — 7y

2 answers

Log in to vote
2
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
7 years ago

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.
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). ReynaldoVictarion 70 — 7y
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. Perci1 4988 — 7y
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. ReynaldoVictarion 70 — 7y
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. Perci1 4988 — 7y
View all comments (2 more)
0
Check my code, please? :( ReynaldoVictarion 70 — 7y
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. Perci1 4988 — 7y
Ad
Log in to vote
0
Answered by 7 years ago

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)

Answer this question