[SOLVED] Vehicle gui isn't working

  1. What do you want to achieve? Making a gui that shows you the health left in the vehicle and in your character while sitting in the driver seat

  2. What is the issue? When sitting in the VehicleSeat / DriverSeat, it just doesn’t appear, debugging I saw that the GUI isn’t even cloning into the PlayerGui

  3. What solutions have you tried so far? I found a similar post, but still didn’t worked. Not too different code from the one I’m showing.

Here is the script I’ve tried so far:

local players = game.Players
local gui = script:WaitForChild("CarGui")

local car = script.Parent
local driveSeat = car:WaitForChild("DriveSeat")
local occupant = driveSeat.Occupant

driveSeat:GetPropertyChangedSignal("Occupant"):Connect(function()
	if occupant then
		local plr = players:GetPlayerFromCharacter(occupant.Parent)
		local newgui = gui:Clone()
		local plrgui = plr:WaitForChild("PlayerGui")
		
		newgui.Parent = plrgui
		
		if newgui.Enabled == false then
			newgui.Enabled = true
		end
		
		local carHealth = newgui:WaitForChild("CarHealth")
		local playerHealth = newgui:WaitForChild("PlayerHealth")
		
		local carHealthLabel = carHealth:WaitForChild("Health")
		local playerHealthLabel = playerHealth:WaitForChild("Health")
		
		local playerHum = plr.Character:WaitForChild("Humanoid")
		local carHum = car:WaitForChild("Humanoid")

		task.wait()
		carHealthLabel.Text = carHum.Health.."/"..carHum.MaxHealth
		playerHealthLabel.Text = playerHum.Health.."/"..playerHum.MaxHealth
		
		carHum.HealthChanged:Connect(function()
			if carHum.Health > 0 then
				task.wait()
				carHealthLabel.Text = carHum.Health.."/"..carHum.MaxHealth
			else
				newgui:Destroy()
			end
		end)
		
		playerHum.HealthChanged:Connect(function()
			if playerHum.Health > 0 then
				task.wait()
				playerHealthLabel.Text = playerHum.Health.."/"..playerHum.MaxHealth
			else
				newgui:Destroy()
			end
		end)
	elseif not occupant then
		local plr = players:GetPlayerFromCharacter(occupant.Parent)
		local plrgui = plr:WaitForChild("PlayerGui")
		local newgui = plrgui:FindFirstChild("CarGui")
		newgui:Destroy()
	end
end)

You need to update your code to the following:

driveSeat:GetPropertyChangedSignal("Occupant"):Connect(function()
	if driveSeat.Occupant then
		
		occupant = driveSeat.Occupant
		
		local plr = players:GetPlayerFromCharacter(occupant.Parent)
		local newgui = gui:Clone()
		local plrgui = plr:WaitForChild("PlayerGui")

		newgui.Parent = plrgui
		
		if newgui.Enabled == false then
			newgui.Enabled = true
		end

		local carHealth = newgui:WaitForChild("CarHealth")
		local playerHealth = newgui:WaitForChild("PlayerHealth")

		local carHealthLabel = carHealth:WaitForChild("Health")
		local playerHealthLabel = playerHealth:WaitForChild("Health")

		local playerHum = plr.Character:WaitForChild("Humanoid")
		local carHum = car:WaitForChild("Humanoid")

		task.wait()
		carHealthLabel.Text = carHum.Health.."/"..carHum.MaxHealth
		playerHealthLabel.Text = playerHum.Health.."/"..playerHum.MaxHealth

		carHum.HealthChanged:Connect(function()
			if carHum.Health > 0 then
				task.wait()
				carHealthLabel.Text = carHum.Health.."/"..carHum.MaxHealth
			else
				newgui:Destroy()
			end
		end)

		playerHum.HealthChanged:Connect(function()
			if playerHum.Health > 0 then
				task.wait()
				playerHealthLabel.Text = playerHum.Health.."/"..playerHum.MaxHealth
			else
				newgui:Destroy()
			end
		end)
	else
		
		local plr = players:GetPlayerFromCharacter(occupant.Parent)
		local plrgui = plr:WaitForChild("PlayerGui")
		local newgui = plrgui:FindFirstChild("CarGui")
		newgui:Destroy()
	end
end)

I believe that you set the variable occupant to the Occupant of the seat at the beginning of the script (server boot I asume) resulting in it being nil. Once the Occupant of the seat got changed, it checks the variable occupant which is still nil so you need to refresh it. Lemme know if it fixed it! :slight_smile:

1 Like

I tried your solution and it’s still not working :frowning:

1 Like

Okay so, in order to narrow down your issue, is it printing anything in the Output? Perhaps something like “Infinite yield possible on ‘’” or so? Also, I tried this code and it printed ran / ran2 based on if I sat / jumped out of the seat. Here is my code:

local players = game.Players
local gui = script:WaitForChild("CarGui")

local car = script.Parent
local driveSeat = game.Workspace.Seat
local occupant = driveSeat.Occupant

driveSeat:GetPropertyChangedSignal("Occupant"):Connect(function()
	if driveSeat.Occupant then
		
		occupant = driveSeat.Occupant
		
		local plr = players:GetPlayerFromCharacter(occupant.Parent)
		local newgui = gui:Clone()
		local plrgui = plr:WaitForChild("PlayerGui")

		newgui.Parent = plrgui

		print("ran")
		
		if newgui.Enabled == false then
			newgui.Enabled = true
		end

		local carHealth = newgui:WaitForChild("CarHealth")
		local playerHealth = newgui:WaitForChild("PlayerHealth")

		local carHealthLabel = carHealth:WaitForChild("Health")
		local playerHealthLabel = playerHealth:WaitForChild("Health")

		local playerHum = plr.Character:WaitForChild("Humanoid")
		local carHum = car:WaitForChild("Humanoid")

		task.wait()
		carHealthLabel.Text = carHum.Health.."/"..carHum.MaxHealth
		playerHealthLabel.Text = playerHum.Health.."/"..playerHum.MaxHealth

		carHum.HealthChanged:Connect(function()
			if carHum.Health > 0 then
				task.wait()
				carHealthLabel.Text = carHum.Health.."/"..carHum.MaxHealth
			else
				newgui:Destroy()
			end
		end)

		playerHum.HealthChanged:Connect(function()
			if playerHum.Health > 0 then
				task.wait()
				playerHealthLabel.Text = playerHum.Health.."/"..playerHum.MaxHealth
			else
				newgui:Destroy()
			end
		end)
	else
		
		print("ran2")
		
		local plr = players:GetPlayerFromCharacter(occupant.Parent)
		local plrgui = plr:WaitForChild("PlayerGui")
		local newgui = plrgui:FindFirstChild("CarGui")
		newgui:Destroy()
	end
end)
1 Like

By the way, I have changed this code snippit to be fitting to your variables. Vehicle gui isn't working - #2 by DivScripts

1 Like

For this script, it is needed to be a server script or it can also be a local script?
Because I updated it and it’s still not working, also, there’s nothing in the output related to the script itself (other than unrelated stuff)

1 Like

Well, this script needs to be inside of the car:

local car = script.Parent
local driveSeat = car:WaitForChild("DriveSeat")
local occupant = driveSeat.Occupant

It looks for the driveSeat within the car (the parent of the script). Local Scripts also do not run in the workspace so it must be a Server-Sided script.

2 Likes

Oh, so that’s why it wasn’t working. But there’s still a problem, when I leave the car it doesn’t destroy the car’s gui :sweat_smile:

1 Like

Ah, I see! Vehicle gui isn't working - #5 by DivScripts

Did you use that code? Since for me it runs ran2 aswell so it shoudl destroy your UI too!

1 Like

Oh, nevermind, I checked the code and it was that I put another “occupant = driveSeat.Occupant” in the last “else” conditional :sweat_smile:

Thank you so much for the help! :wink:

1 Like

Noo problem! Have an amazing day. :slight_smile:

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.