Hello, I am trying to break the loop when proj stops existing, when the part is destroyed. I thought checking if it isn't nil would work but it's never nil and I can't check the parent folder for the child since it can contain multiple of them at once. I could set it to nil in the part of the script that will destroy it (for context it's a projectile that can get shot down by anti air) but coming up with a solution without that would be preferable for manual testing.
Thank you for any answers.
local W, RS, SS = game.Workspace, game:GetService("ReplicatedStorage"), game:GetService("ServerStorage") local r = RS:WaitForChild("Launch") local l = W.Launcher local pF = W.Projectiles local function Launch(plr, pos) local proj = SS.Torpedo:Clone() proj.Parent = pF local p1 = proj.Position local p3 = pos local p2 = p1:lerp(p3, 0.5) + Vector3.new(0,50,0) l.CFrame = CFrame.new(l.Position, Vector3.new(pos.X,l.Position.Y,pos.Z)) l.Shoot:Play() p.Sound:Play() for t = 0,1,0.01 do if proj ~= nil then local TP = QuadraticBezier(t , p1, p2, p3) local TO = QuadraticBezier(t + 0.005 , p1, p2, p3) proj.CFrame = CFrame.new(TP, TO) wait() end end end r.OnServerEvent:Connect(Launch)
Reffering to this post https://devforum.roblox.com/t/how-to-check-if-an-object-exists/1327161/19 it seems like this has no solution as there is no way to differentiate between instances with the same name and I'll have to number them. I'm sharing my solution.
Function to check if proj exists, they have been numbered.
local function existCheck(proj) if pF:FindFirstChild(proj.Name) then return(true) end end
Final loop and numbering.
local num = 0 local function Launch(plr, pos) local p = SS.Missile:Clone() p.Parent = pF p.Name = "Missile" .. num num = num + 1 for t = 0,1,0.01 do if existCheck(p) then --loop until the end of until proj doesn't exist wait() end end end
does this work for you?
for t = 0,1,0.01 do if not proj then break elseif proj ~= nil then -- your code end
either that, or a more logical way
for t = 0,1,0.01 do if proj ~= nil then -- your code else break end