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.
01 | local W, RS, SS = game.Workspace, game:GetService( "ReplicatedStorage" ), game:GetService( "ServerStorage" ) |
02 | local r = RS:WaitForChild( "Launch" ) |
03 | local l = W.Launcher |
04 | local pF = W.Projectiles |
05 |
06 | local function Launch(plr, pos) |
07 | local proj = SS.Torpedo:Clone() |
08 | proj.Parent = pF |
09 | local p 1 = proj.Position |
10 | local p 3 = pos |
11 | local p 2 = p 1 :lerp(p 3 , 0.5 ) + Vector 3. new( 0 , 50 , 0 ) |
12 | l.CFrame = CFrame.new(l.Position, Vector 3. new(pos.X,l.Position.Y,pos.Z)) |
13 | l.Shoot:Play() |
14 | p.Sound:Play() |
15 |
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.
1 | local function existCheck(proj) |
2 | if pF:FindFirstChild(proj.Name) then |
3 | return ( true ) |
4 | end |
5 | end |
Final loop and numbering.
01 | local num = 0 |
02 | local function Launch(plr, pos) |
03 | local p = SS.Missile:Clone() |
04 | p.Parent = pF |
05 | p.Name = "Missile" .. num |
06 | num = num + 1 |
07 |
08 | for t = 0 , 1 , 0.01 do |
09 | if existCheck(p) then |
10 | --loop until the end of until proj doesn't exist |
11 | wait() |
12 | end |
13 | end |
14 | end |
does this work for you?
1 | for t = 0 , 1 , 0.01 do |
2 | if not proj then break elseif proj ~ = nil then |
3 | -- your code |
4 | end |
either that, or a more logical way
1 | for t = 0 , 1 , 0.01 do |
2 | if proj ~ = nil then |
3 | -- your code |
4 | else |
5 | break |
6 | end |