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
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
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)
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!
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)
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)
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.