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.
01 | script.Parent.VehicleSeat.Active.Changed:connect( function () |
02 |
03 | if script.Parent.VehicleSeat.Active.Value = = false then |
04 |
05 | repeat |
06 | wait( 1 ) |
07 | script.Parent.Number.Value = script.Parent.Number.Value - 1 |
08 | script.Parent.Base.Countdown.Number.Text = script.Parent.Number.Value |
09 |
10 | if script.Parent:FindFirstChild( "VehicleSeat" ) = = nil then |
11 | script.Parent:Destroy() |
12 | elseif script.Parent:FindFirstChild( "Number" ) and script.Parent.Number.Value < 1 then |
13 | script.Parent:Destroy() |
14 | end |
15 |
16 | until script.Parent.VehicleSeat.Active.Value = = true or script.Parent.VehicleSeat = = nil |
17 |
18 | end |
19 | 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?
Here's what I recommend you do.
01 | local carDestroyed = false |
02 |
03 | script.Parent.VehicleSeat.Active.Changed:connect( function () |
04 |
05 | if script.Parent.VehicleSeat.Active.Value = = false then |
06 |
07 | repeat |
08 | wait( 1 ) |
09 | script.Parent.Number.Value = script.Parent.Number.Value - 1 |
10 | script.Parent.Base.Countdown.Number.Text = script.Parent.Number.Value |
11 |
12 | if script.Parent:FindFirstChild( "VehicleSeat" ) = = nil then |
13 | script.Parent:Destroy() |
14 | carDestroyed = true |
15 | elseif script.Parent:FindFirstChild( "Number" ) and script.Parent.Number.Value < 1 then |
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!
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.
01 | script.Parent.VehicleSeat.Active.Changed:connect( function () |
02 |
03 | if script.Parent.VehicleSeat.Active.Value = = false then |
04 |
05 | while script.Parent.VehicleSeat.Active.Value = = false do |
06 | wait( 1 ) |
07 | if script.Parent.VehicleSeat.Active.Value = = false then -- just another clumsy failsafe in case the value changes within that 1 second tick. |
08 | if script.Parent.Number.Value = = 0 then |
09 | script.Parent:Destroy() |
10 | 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. |
11 | script.Parent.Number.Value = script.Parent.Number.Value - 1 |
12 | script.Parent.Base.Countdown.Number.Text = script.Parent.Number.Value |
13 | end |
14 | end |
15 | 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!