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.

01script.Parent.VehicleSeat.Active.Changed:connect(function()
02 
03if script.Parent.VehicleSeat.Active.Value == false then
04 
05repeat
06wait(1)
07script.Parent.Number.Value = script.Parent.Number.Value - 1
08script.Parent.Base.Countdown.Number.Text = script.Parent.Number.Value
09 
10if script.Parent:FindFirstChild("VehicleSeat") == nil then
11script.Parent:Destroy()
12elseif script.Parent:FindFirstChild("Number") and script.Parent.Number.Value < 1 then
13script.Parent:Destroy()
14end
15 
16until script.Parent.VehicleSeat.Active.Value == true or script.Parent.VehicleSeat == nil
17 
18end
19end)

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.

01local carDestroyed = false
02 
03script.Parent.VehicleSeat.Active.Changed:connect(function()
04 
05if script.Parent.VehicleSeat.Active.Value == false then
06 
07repeat
08wait(1)
09script.Parent.Number.Value = script.Parent.Number.Value - 1
10script.Parent.Base.Countdown.Number.Text = script.Parent.Number.Value
11 
12if script.Parent:FindFirstChild("VehicleSeat") == nil then
13script.Parent:Destroy()
14carDestroyed = true
15elseif script.Parent:FindFirstChild("Number") and script.Parent.Number.Value < 1 then
View all 23 lines...

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.

01script.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
View all 21 lines...

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