How to get player from seat?

I have spent hours looking for information, this is the best version of the script i did
I see that occupant is a string but i dont know any other option

Output:
changedsignal - Server - Script:9
cargui cloned - Server - Script:11
Workspace.Seat.Script:8: attempt to index nil with ‘Parent’ - Server - Script:8

For some reason the script error only appeared on the second trigger

local Players = game:GetService("Players")

local Seatf = script.Parent

local varieble = true

Seatf:GetPropertyChangedSignal("Occupant"):Connect(function()
	local player = Players:GetPlayerFromCharacter(script.Parent.Occupant.Parent)
	print("changedsignal")
	if varieble == true then
		print("cargui cloned")
		player.PlayerGui.CarGui.Enabled = true
		varieble = false
	else
		player.PlayerGui.CarGui.Enabled = false
		varieble = true
	end
end)
1 Like

Attributes sound like a fine solution.

local Players = game:GetService("Players")
local seat = script.Parent

seat:GetPropertyChangedSignal("Occupant"):Connect(function()
	if seat.Occupant then
		local player = Players:GetPlayerFromCharacter(seat.Occupant.Parent)
		seat:SetAttribute("Player", player.UserId) -- instances not supported
		-- continue
	else
		local player = Players:GetPlayerByUserId(seat:GetAttribute("Player"))
		print(player.Name .. " was here")
	end
end)

The error on the 8th row stays the same
I think occupant is a string and you cant get parent from a string

I’m sorry, but I can’t replicate that issue. seat.Occupant is always humanoid. The code works flawlessly on my end, even when I reset while sitting.

Look at line 6 in my example. It only runs if occupant exists. When player stands up, it becomes nil and your code errors by attempting to index it with .Parent.

1 Like

Oh thanks, i added " if seat.Ocuppant then " and now everything works perfectly!

1 Like

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