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

Car Repeat Statement Keeps Creating Errors?

Asked by 7 years ago
Edited 7 years ago

Alright, so this is my script where whenever you jump out of a car (Active = false) then it begins to countdown from 30. The "until" part keeps saying "attempt to index field 'Parent' (a nil value)" when the car (script.Parent) gets destroyed.


script.Parent.VehicleSeat.Active.Changed:connect(function() if script.Parent.VehicleSeat.Active.Value == false then repeat wait(1) script.Parent.Number.Value = script.Parent.Number.Value - 1 script.Parent.Base.Countdown.Number.Text = script.Parent.Number.Value if script.Parent:FindFirstChild("VehicleSeat") == nil then script.Parent:Destroy() elseif script.Parent:FindFirstChild("Number") and script.Parent.Number.Value < 1 then script.Parent:Destroy() end until script.Parent.VehicleSeat.Active.Value == true or script.Parent.VehicleSeat == nil end end)

This happens because once the car is removed, the parent is no longer available. But how do I make the repeat statement stop so I don't get any errors?

0
Then why won't you remove the Destroy() function and try to use another method? Because, I'll be honest, for me, the question was a bit confused, and therefor, I didn't understand it, so, what I'm asking is, **can you be a bit more specific**? OfcPedroo 396 — 7y
0
scripts that call Destroy() on themselves or their Parent object also tend to be problematic more often than not. DropshipPilot 148 — 7y

2 answers

Log in to vote
2
Answered by 7 years ago

Here's what I recommend you do.

local carDestroyed = false

script.Parent.VehicleSeat.Active.Changed:connect(function()

if script.Parent.VehicleSeat.Active.Value == false then

repeat
wait(1)
script.Parent.Number.Value = script.Parent.Number.Value - 1
script.Parent.Base.Countdown.Number.Text = script.Parent.Number.Value

if script.Parent:FindFirstChild("VehicleSeat") == nil then
script.Parent:Destroy()
carDestroyed = true
elseif script.Parent:FindFirstChild("Number") and script.Parent.Number.Value < 1 then
script.Parent:Destroy()
carDestroyed = true
end

until carDestroyed == true

end
end)


So how this works is we create a variable called carDestroyed and we set it to false since the car has not been destroyed yet. In your loop we make it repeat until our variable carDestroyed is true. Now, in order for it to become true, we set it to true once we destroy the car. Since we are not referencing the car that got destroyed you shouldn't then get any errors. I hope this helps!

0
This is likely a more streamlined solution than my answer. Good job! DropshipPilot 148 — 7y
0
I didn't even see your answer. Looks like we both kind of posted it at the same time. Your answer is still pretty good though. Nice job! thebootsie123 160 — 7y
Ad
Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

My revised answer:

The problem was, you had a "repeat" loop that would still try to look for things after it had destroyed them.

With as little modification to your code as possible, I seem to have been able to rectify the problem by using a "While" loop- "while VehicleSeat.Active == true do".

It's still not the best way of handling the countdown, but it's the best considering the NumberValues are there for a reason (probably context outside of the code you've posted)

This is the code to my best-guess solution. Give it a shot.

script.Parent.VehicleSeat.Active.Changed:connect(function()

    if script.Parent.VehicleSeat.Active.Value == false then

        while script.Parent.VehicleSeat.Active.Value == false do
            wait(1)
            if script.Parent.VehicleSeat.Active.Value == false then -- just another clumsy failsafe in case the value changes within that 1 second tick.
                if script.Parent.Number.Value == 0 then 
                    script.Parent:Destroy()
                else -- By doing the counting down in an "else" statement, we make sure we never have to try and count down a numbervalue that no longer exists.
                    script.Parent.Number.Value = script.Parent.Number.Value - 1
                    script.Parent.Base.Countdown.Number.Text = script.Parent.Number.Value
                end
            end
        end

    else
        script.Parent.Number.Value = 30 -- if active has been changed to "true", it resets your countdown.
    end

end)

I'll edit this answer if I manage to create a countdown that reacts instantly to the change of VehicleSeat.Active.

Hope this helps!

Answer this question