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

How to break a loop as cloned part stops existing? [Solved]

Asked by 3 years ago
Edited by JesseSong 3 years ago

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.

01local W, RS, SS = game.Workspace, game:GetService("ReplicatedStorage"), game:GetService("ServerStorage")
02local r = RS:WaitForChild("Launch")
03local l = W.Launcher
04local pF = W.Projectiles
05 
06local function Launch(plr, pos)
07    local proj = SS.Torpedo:Clone()
08    proj.Parent = pF
09    local p1 = proj.Position
10    local p3 = pos
11    local p2 = p1:lerp(p3, 0.5) + Vector3.new(0,50,0)
12    l.CFrame = CFrame.new(l.Position, Vector3.new(pos.X,l.Position.Y,pos.Z))
13    l.Shoot:Play()
14    p.Sound:Play()
15 
View all 26 lines...

2 answers

Log in to vote
1
Answered by 3 years ago
Edited 3 years ago

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.

1local function existCheck(proj)
2    if pF:FindFirstChild(proj.Name) then
3        return(true)
4    end
5end

Final loop and numbering.

01local num = 0
02local 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
14end
Ad
Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

does this work for you?

1for 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

1for t = 0,1,0.01 do
2    if proj ~= nil then
3        -- your code
4    else
5        break
6    end
0
Sadly doesn't work, the loop carries out until it's finished. I feel like it has something to do with how proj is defined, it always exists even if you destroy the instance, but it seems like the variable stays no matter the instance. Adsteriuss 10 — 3y
0
alright, if it doesn't work, try checking if proj exists using pF:WaitForChild(proj,1)? also, if it still doesn't work, try changing "proj" to the name of the object in the DataModel. Persona3_FES 2 — 3y
0
I tried both and more and no success either. The problem is that you can't look for the child using a instance, and if I search with the string of projs name, it could just return other projs in the same folder as there can be more at once. Adsteriuss 10 — 3y
0
well, sorry then, i cant help you then :( i hope someone that can finds this topic Persona3_FES 2 — 3y
View all comments (2 more)
0
I just realised my mistake, I've been deleting the proj on the client side during testing the whole time, therefore it's never been deleted. I was just about to implement numbering and realised that as FindFirstChild was finding it even thought i deleted it. I'm used to old studio when everything was serverside lmao, I got baited again. Adsteriuss 10 — 3y
0
hah, happens all of the time :) Persona3_FES 2 — 3y

Answer this question