How can I get the player?

Hello,

So I am trying to make it so that when someone sits onto a seat a UI pops up (script runs) but the UI only comes up on the player who is on the seat. The script I have at the works but it does it for everyone because it’s a server script and aiming towards the whole server. I am guessing what you need to do is get the player so you can get the PlayerGui but not sure on how I can get the player from my script. (the script below is just the idea of how the seat system works)

Script:

local seat = workspace.Seat 

seat.DescendantAdded:Connect(function(p)
    if p.ClassName == "Weld" then
        print("PlayerHasSat") 
    end
end)

seat.DescendantRemoving:Connect(function(r)
    if r.ClassName == "Weld" then
        print("PlayerHasStood") 
    end
end)

You could use a LocalScript inside StarterCharacterScripts that checks when the character sits. This single script could work for different seats if you ever want to use different guis as well.

You can use a combination of .Changed and the Occupant property.

local seat = workspace.Seat
local player

seat.Changed:Connect(function()
   if seat.Occupant == nil then return end
   if game.Players:FindFirstChild(seat.Occupant.Parent.Name) then
      player = game.Players[seat.Occupant.Parent.Name]
   end
end)

Where the script is the child of the seat:

local LastOccupant = nil
script.Parent:GetPropertyChangedSignal("Occupant"):Connect(function()
	if LastOccupant ~= nil and script.Parent.Occupant == nil then
		print(LastOccupant.Parent.Name.." has just stood from the seat.")
		LastOccupant = nil
	elseif script.Parent.Occupant ~= nil then
		LastOccupant = script.Parent.Occupant
		print(script.Parent.Occupant.Parent.Name.." has just sat on the seat.")
		local SeatedPlayer = game.Players:GetPlayerFromCharacter(script.Parent.Occupant.Parent)
		if SeatedPlayer ~= nil then
			print(SeatedPlayer.UserId)
		end
	end
end)

But then how does it remove when they sit off the chair?

Here is the link the wiki reference of a Seat object. The code is very similar to that of @kylerzong’s above.

This is their example of how to detect when a player has sat or gotten up off of a Seat (this was copied straight from the link above – I just edited it to fit your example).

        local Players = game:GetService("Players")
        local seat = workspace.Seat 
        
        local currentPlayer = nil
        
        seat:GetPropertyChangedSignal("Occupant"):Connect(function()
        	local humanoid = seat.Occupant 
        	if humanoid then 
        		local character = humanoid.Parent
        		local player = Players:GetPlayerFromCharacter(character)
        		if player then 
        			print(player.Name.." has sat down")
        			currentPlayer = player
        			return
        		end	
        	end
        	if currentPlayer then 
        		print(currentPlayer.Name.." has got up")
        		currentPlayer = nil
        	end
        end)

They used a variable called currentPlayer that is how they detect who the current ocupant of the Seat is, and the seat:GetPropertyChangedSignal("Occupant"):Connect(function() is the event that fires on the Seat when someone either a) sits or b) gets up.

Hope this helps!

1 Like