Need help with a vehicle regen system

Hi, I’m working on a vehicle system for my game, but there is a problem.

I’m working on the regen system right now and I’m trying to make it so that the vehicle cannot be regened if it is occupied, I’ve tried several ways, but none were successful.

My script:

local driverSeat = script.Parent.Parent.Tank.DriverSeat
local driverSeatOccupant = driverSeat.Occupant
local TxtBtn = script.Parent.SurfaceGui.TextButton

TxtBtn.MouseButton1Click:Connect(function()
	if DriverSeat == nil then
		print("Tank Unoccupied!")
		script.Parent.Parent.Tank:Destroy()
		tankToRegen:Clone().Parent = workspace.Vehicles[tankName]
	else
		print("Tank Occupied!")
		return
	end
end)

Regardless if it is occupied or not it will always go with the ‘Tank Unoccupied’ part of the script. Help would be greatly appreciated!

OnlyZeroZs

Could you try this?

local driverSeat = script.Parent.Parent.Tank.DriverSeat
local driverSeatOccupant = driverSeat.Occupant
local TxtBtn = script.Parent.SurfaceGui.TextButton

TxtBtn.MouseButton1Click:Connect(function()
    print("Button pressed")
	if driverSeatOccupant == nil then
		print("Tank Unoccupied!")
		script.Parent.Parent.Tank:Destroy()
		tankToRegen:Clone().Parent = workspace.Vehicles[tankName]
	else
		print("Tank Occupied!")
		return
	end
end)

I noticed you made the if DriverSeat == nil capatilized, so I went ahead & fixed that :thinking:

That didn’t work, it still thinks the tank is unoccupied at all times.

Hm, alright what about this?

local driverSeat = script.Parent.Parent.Tank.DriverSeat
local TxtBtn = script.Parent.SurfaceGui.TextButton

TxtBtn.MouseButton1Click:Connect(function()
    print("Button pressed")
    print(driverSeat.Occupant)

	if driverSeat.Occupant == nil then
		print("Tank Unoccupied!")
		script.Parent.Parent.Tank:Destroy()
		tankToRegen:Clone().Parent = workspace.Vehicles[tankName]
	else
		print("Tank Occupied!")
		return
	end
end)

Nope, It still registers that its unoccupied.

Really? What did the second print statement give you?

  1. Button Pressed
  2. nil
  3. Tank Unoccupied!

I think it would be better if you do the checking when the mouse is being clicked. Here’s an example of how it would look like:

Instead of:

local driver = script.Parent.DriverSeat.Occupant

TextButton.MouseButton1Click:Connect(function()
    if driver ~= nil then
        print("we have driver")
    else 
        print("no")
    end
end)

How about:

TextButton.MouseButton1Click:Connect(function()
    local driver = script.Parent.DriverSeat.Occupant
    if driver ~= nil then
        print("yes driver")
    else
        print("no driver")
    end
end)
3 Likes

Yes! That worked, thank you, Now I can get back to work on my game!

Remember, your script doesn’t run in a loop, once it past a statement, whatever that’s inside the statement will be that forever.