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

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

	local player = Players:GetPlayerFromCharacter(script.Parent.Occupant.Parent)
	if varieble == true then
		print("cargui cloned")
		player.PlayerGui.CarGui.Enabled = true
		varieble = false
		player.PlayerGui.CarGui.Enabled = false
		varieble = true

Attributes sound like a fine solution.

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

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

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.