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 2 years ago
Edited by JesseSong 2 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.

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)

2 answers

Log in to vote
1
Answered by 2 years ago
Edited 2 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.

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
Ad
Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

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
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 — 2y
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 — 2y
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 — 2y
0
well, sorry then, i cant help you then :( i hope someone that can finds this topic Persona3_FES 2 — 2y
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 — 2y
0
hah, happens all of the time :) Persona3_FES 2 — 2y

Answer this question