How would I get the username of someone sitting on a specific seat?

  1. What do you want to achieve?
    I am trying to make a script that when I click on a surfacegui button, it will tell me who is currently sitting on the seat.

  2. What is the issue?
    I can’t seem to script it to make it function. Please note, I am not a professional scripter; I am still learning. I looked at several topics on devforum and robloxdev.

  3. What solutions have you tried so far?

local function OnClicked()

local occupant = nil -- Nobody is sitting on the seat by default.
local seat = game.Workspace.Seat1

script.Parent:GetPropertyChangedSignal("Occupant"):connect(function()
    if seat.Occupant then -- If there now is an occupant
        occupant = game.Players:FindFirstChild(script.Parent.Occupant.Parent.Name)
		print("Patient is sitting down!")
        
    elseif not seat.Occupant then -- If there is no occupant
        occupant = nil
		print("No one is sitting down!")

end
end)
end

script.Parent.MouseButton1Click:connect(OnClicked)
3 Likes

If you only want it so that it updates when someone clicks the button you want to avoid using the GetPropertyChangedSignal method. Instead, you can simply check the Occupant of the seat when the button is clicked. Since we know that Occupant is a Humanoid instance, we can use GetPlayerFromCharacter with the Humanoid’s Parent (Player’s Character).

local Seat = workspace.Seat1

script.Parent.MouseButton1Click:Connect(function()
	local Humanoid = Seat.Occupant
	if Humanoid then
		local Player = game.Players:GetPlayerFromCharacter(Humanoid.Parent)
		print(Player.Name .. " sat in the seat.")
	else
		print("No one is sitting down!")
	end
end)
4 Likes

Thank you so much! Your explanation made sense and it works perfectly.

I appreciate your reply, this was really helpful. I tend to forget that :connect is no longer useful.

1 Like