So i’m making a script where the ball respawns when it is nil or destroyed. Yes it works at first so it spawns. but when i drop it off the map it doesn’t???
local RespawnPoint = script.Parent
local Ball = script.Parent.ClonedBall.Value
local CurrentBall = nil
local NewBall = nil
while wait() do
if CurrentBall == nil then
local BallClone = Ball:Clone()
BallClone.Position = RespawnPoint.Position
BallClone.Parent = workspace.Balls
NewBall = BallClone
CurrentBall = NewBall
end
end
local RespawnPoint = script.Parent
local Ball = script.Parent.ClonedBall.Value
local CurrentBall = nil
local NewBall = nil
while wait() do
if CurrentBall == nil or CurrentBall.Parent == nil then
local BallClone = Ball:Clone()
BallClone.Position = RespawnPoint.Position
BallClone.Parent = workspace.Balls
NewBall = BallClone
CurrentBall = NewBall
end
end
It’s worth noting that this is probably better to make event based. Overall it’s not too processing intensive to just loop, but with this you can just respawn it once it’s been destroyed instead of checking over and over.
local Ball = script:WaitForChild("Ball")
local ActiveBall
local connection
local function CreateBall(child, parent)
if not ActiveBall or parent == nil then
if connection then connection:Disconnect() end --Not technically needed because the ball getting destroyed would call this
ActiveBall = Ball:Clone()
ActiveBall.Position = Vector3.new(0,10,0)
ActiveBall.Parent = game.Workspace
connection = ActiveBall.AncestryChanged:Connect(CreateBall)
end
end
CreateBall()