I am trying to make a script which will despawn a vehicle after not being driven for over 10 seconds. I have attached the code below. The first time I drive the vehicle and exit the driverseat the whole script works perfectly. Once I re-enter the vehicle, and then exit, the textlabel doesn’t become visible anymore and I am just left with what looks like an infinite loop of printing “end of loop”.
Script (Server)
local jeep = script.Parent.Parent.Parent.Parent
local breakloops = false
local jeepActivated = false
while jeepActivated == false do
wait()
if script.Parent.Parent.Parent.Occupant == nil then
else
jeepActivated = true
end
end
while wait() do
if jeep.DriveSeat.Occupant == nil then
script.Parent.Visible = true
for i = 10,0,-1 do
wait(1)
script.Parent.Text = "DESPAWNS IN "..i
if jeep.DriveSeat.Occupant == nil then
else
breakloops = true
script.Parent.Parent.Enabled = false
end
if breakloops == true then
break
end
if i == 1 then
jeep:Destroy()
end
end
end
print("end of loop")
end
Reading over the script once again, I think I found my problem. I will keep you all updated if I need help.
For anyone using this as a resource and is interested in what I found:
#1: I had the breakloops variable at the top of the script making it global. This meant that whenever the for loop began, after setting the variable equal to true the for loop would break everytime. #2: I was setting the textlabel to visible, followed by setting the BillboardGui to not enabled meaning that the second time the label could not become visible as the gui was disabled.
local TextLabel = script.Parent
local Seat = script:FindFirstAncestorOfClass("VehicleSeat") --Change this if a "Seat" instance is being used instead.
local OccupantChanged = Seat:GetPropertyChangedSignal("Occupant")
local Jeep = script:FindFirstAncestor("Jeep") --Change to name of Jeep.
local function OnSeatOccupantChanged()
if not Seat.Occupant then
TextLabel.Visible = true
for Count = 10, 0, -1 do
task.wait(1)
if Seat.Occupant then
TextLabel.Visible = false
TextLabel.Text = ""
return
end
TextLabel.Text = "Jeep despawns in"..Count.." seconds!"
end
Jeep:Destroy()
end
end
OccupantChanged:Connect(OnSeatOccupantChanged)
If you need anything explaining let me know. I also changed the gui disabling part and made it so that the textlabel is hidden instead, this way if a player gets off the seat, gets on the seat and gets off the seat again the textlabel will still appear as the gui hasn’t been disabled.