Vehicle Owning Script

Hello. Attempting to script a another vehicle-owning system, I failed.

script.Parent.Touched:Connect(function()
	local occ script.Parent.Occupant
	if occ == nil then
		return
	else
		for _, billboard in pairs(script.Parent.Parent:GetDescendants()) do
			if billboard.Name == "OccupantGui" then
				billboard.TextLabel = occ.Name .. " is driving this vehicle."
			end
		end
	end
end)

I also want these to be put into the script:

  • Detecting if owner leaves/resets, if they did the vehicle would be vacant again

Explorer Properties:
image
image

4 Likes

The first thing I’ve noticed is that you’re using .Touched instead of .Changed when waiting for the occupant. You should change that.

Instead of return, you should put the billboard GUI to say that nobody is in the vehicle.

3 Likes

Alright I have done the changes.
But I am getting this redline in line 2.
image
EDIT: ok nvm i forgot the =
ANOTHER EDIT: i got this error?

1 Like

You’re setting the TextLabel, not the Textlabel.Text. The error occurs since it thinks you are refrencing TextLabel as a property of BillboardGui called TextLabel instead of the actual instance.

just change

billboard.TextLabel = occ.Name .. " is driving this vehicle."

to

billboard.TextLabel.Text = occ.Name .. " is driving this vehicle."

and it SHOULD work

1 Like

Ok that fixed the problem, but uh…
image
Why is it displaying “Humanoid” instead of the display name of the player…

1 Like

I’m not familiar with occupants but it seems like the occupant is set to the humanoid, if that’s the case use occupant.Parent.Name (yeah occupants are set to the humanoid)

I ended up using occupant.DisplayName because I wanted it to display the player’s display name.

1 Like

That’s fine, glad I could help

1 Like

Uh…
Every time the player exits the vehicle, it becomes vacant again.
I want it to be vacant only if the player leaves the game or resets.

1 Like

I don’t work with vehicles often, so I don’t know much about helping on this, but I’ll give you a list of things to try,

But what do you mean by this? Like, you want them to own the vehicle even after they get up? Or you want the occupant to be equal to the player still

1 Like

I want the occupant to be equal to the player still, until they reset/leave.

Well this isn’t possible, but what would you use this behavior for? I’m quite sure there are other workarounds like objectvalues and stuff

1 Like

I have a stringvalue in the BillboardGui.

1 Like

(this may be slightly unoptimal as i’m inexperienced in this but)
Basically like an owner value, you can make it not change when the occupant is changes, then simply detect whenever someone enters the seat and

  • Check if there’s a value to the stringvalue
    If not, then there is no occupant and you can let the player sit down
  • else
    Boot them off the seat

When the value is set, just detect if the player dies or leaves (through connections that have variables), then if so, set the stringvalue’s value to nil then disconnect both connections

Alternatively to a stringvalue, you can use an objectvalue and set the value to the player’s humanoid and check if the humanoid.parent attempting to sit down has the same name as the value.name (if there’s a value)

1 Like

How would I detect if the player dies/leaves through a ServerScript?

1 Like

Through the Humanoid for death, I have no idea how you’d check for leaving. One thought on how to do this: Every time a player is removed, check if it’s the player, if it is the player, loop through every car in the workspace and check if the value of the car occupant is that player, if so set it to nil

i also found in another post: “A player instances parent should never change in normal circumstances. so if it does you can reliable say that the player left the game.” so unless you have some wacky voodoo code, this should work aswell

local connection

connection = player:GetPropertyChangedSignal("Parent"):Connect(function()
   --plr left
connection:Disconnect()
end)

For death:

local hum = --yourhumanoid

local deathConnection

deathConnection = hum.Died:Connect(function()
    --humanoid died, set the value to nil
deathConnection:Disconnect()
end

youll need to tweak these a bit, and disconnect them in different places if you need, but thats the basics, its 3am for me so im gonna go, if you have any more questions ill answer tomorrow, or a better coder than me will probably give you better methods than I just did lol

1 Like

I improved the script:

script.Parent.Touched:Connect(function()
	local occ = script.Parent.Occupant
	for _, billboard in pairs(script.Parent.Parent:GetDescendants()) do
		if billboard.Name == "OccupantGui" then
			billboard.TextLabel.Text = occ.DisplayName .. "'s Vehicle"
		end
	end
	script.Occupant.Value = occ.Parent.Name
end)

local deathConnection

for _, player in pairs(game.Players:GetDescendants()) do
	if player:IsA("Humanoid") and player.Parent.Name == script.Occupant.Value then
		deathConnection = Player.Died:Connect(function()
			script.Occupant.Value = "NoOccupant"
			deathConnection:Disconnect()
		end)
	end
end

script.Occupant:GetPropertyChangedSignal("Value"):Connect(function()
	if script.Occupant.Value == "NoOccupant" then
		for _, billboard in pairs(script.Parent.Parent:GetDescendants()) do
			if billboard.Name == "OccupantGui" then
				billboard.TextLabel.Text = "Vacant Vehicle"
			end
		end
	end
end)

It still does not detect if occupant resets.
I got this error too:

what’s the script’s parent?, also the error is caused due to there being no occupant. I wouldn’t use .Touched to change the billboardgui, simply just:

  • Check for the occupant value of the ACTUAL seat changing
  • Check if an occupant exists
  • There’s an occupant? Change the value of the string and start the death connection.
script.Parent:GetPropertyChangedSignal("Occupant"):Connect(function()
	if script.Parent.Occupant then -- Check if there's an occupant.
		-- There's an occupant! Make sure that the occupant stringvalue has no value, or other players will be able to get in.
		local occupant = script.Parent.Occupant
		if script.Occupant.Value == "" or script.Occupant.Value == occupant.Parent.Name then
			
			script.Occupant.Value = occupant.Parent.Name
			
			local deathConnection

			deathConnection = occupant.Died:Connect(function()
				script.Occupant.Value = ""
				deathConnection:Disconnect()
			end )
		else
			print("occupant not owner")
			-- Do whatever you want, boot them off of the seat, this occupant doesn't own the vehicle!
		end
	else
		-- There's no occupant! You can change the GUI to say Vacant Vehichle, or whatever else you were doing with the other GetPropertyChangedSignal
	end
end)

This code is tested by me and should detect players that aren’t the owner, it also resets on death.

1 Like
local Seat = script.Parent

Seat:GetPropertyChangedSignal("Occupant"):Connect(function()
	if Seat.Occupant then
		local Player = game.Players:GetPlayerFromCharacter(Seat.Occupant.Parent)
	end
end)

As you might of noticed already, Occupant is a humanoid. This listens when a player enters / leaves. This also fires when the humanoid removes from the workspace.

1 Like

Oops, didn’t meant to reply. Anyways, let me know if that helps.

1 Like