How to get the player's character or name or whatever whenever they jump from their seat

  1. So, i was doing some coding then i realize :
-- Get a reference to the seat
local seat = script.Parent -- Assuming the script is inside the seat

-- Get the Players service
local Players = game:GetService("Players")

-- Listen for changes to the Occupant property
seat:GetPropertyChangedSignal("Occupant"):Connect(function()
	-- Check if the seat is now empty
	if seat.Occupant == nil then
		-- Get the player who left the seat (if there was one)
		local previousOccupant = seat.Occupant
		if previousOccupant then
			local player = Players:GetPlayerFromCharacter(previousOccupant.Parent)
			if player then
				print(player.Name .. " left the seat.")
				-- Add your code here to handle the player leaving the seat
			end
		end
	end
end)

this is the code i found online to find the player who left the seat, but then i realize, hey how can this script get the player if the humanoid is not there anymore. AFterward, my mind ask, how do i make a script that find the player who left the seat. I search everywhere, but nothing. I try to play it but what it printed is nothing

So , anyone can help me?

i first thought to store the player in an array or dictionary, and if the seat is nil, it will check which player humanoid sit is false. But idk

Hello!
It should look something like this:

-- Get a reference to the seat
local seat = script.Parent -- Assuming the script is inside the seat

-- Get the Players service
local Players = game:GetService("Players")
local savedOccupant
local nameOfOccupant
-- Listen for changes to the Occupant property
seat:GetPropertyChangedSignal("Occupant"):Connect(function()
	-- Check if the seat is now empty
	if seat.Occupant ~= nil then
		

		-- Get the player who left the seat (if there was one)
		savedOccupant = seat.Occupant.Parent
		nameOfOccupant = savedOccupant.Name
	elseif seat.Occupant == nil then
		print(nameOfOccupant.." has left the seat.")
		
	

	end
end)

Have a nice time!

1 Like

oh yeah , i forgot you can do that. thanks man!

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