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

How do I clone something from Lighting when a certain object from Workspace is removed?

Asked by 10 years ago

I tried to clone a Soccer ball from Lighting to Workspace when the ball gets deleted but it dosen't work.

wait(3) if game.Workspace.SoccerBall==nil then

local newball = game.Lighting.Ball:Clone() newball.Parent = game.Workspace newball.Position = Vector3.new(41.701, 7.071, 626.308) end

2 answers

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

Scubadoo2's answer is correct and takes the direction you initially suggested.


There's another option, which uses the ChildRemoved event:

function MakeNewBall()
    local newball = game.Lighting.Ball:Clone() 
    newball.Parent ...
    ...
end

game.Workspace.ChildRemoved:connect( function(obj)
    if obj.Name == "SoccerBall" then
        MakeNewBall()
    end
end)

Whenever any object is removed from the workspace, we check if it was called "SoccerBall". In that case, we make the new one.

Ad
Log in to vote
1
Answered by
Scubadoo2 115
10 years ago

The reason why it isn't working is because... well... soccerball doesn't exist when it is removed, so it is trying to access something that isn't there. it will throw the error without seeing that it is checking for something.

To fix this, there is a command called :FindFirstChild that will return nil if it doesn't exist. so you may want to do wait(3) if game.Workspace:FindFirstChild("SoccerBall")==nil then.

Answer this question