Trouble using Changed:Connect Event on a seat

-Hello everyone, I am having some trouble using Seat.Changed:Connect
(Seat being the script parent)

  1. What do I want to achieve?
    -I want to make a chair that, when seated the player gets his team changed to another one. However, the script wont change the team, if there is already one player in it.
    (But only the event connected to the function is causing me trouble)

  2. What is the issue?
    -When I use the event Seat.Changed:Connect to connect the function that I will show down later, this happens.
    Problem

  3. What solutions have you tried so far?
    -I have found out that using an event that activates the function when the seat is touched everything works well, however it appears to only fire when the player leaves the chair.
    (Other methods like using click detectors worked without a problem.)

Not understanding why the event may cause a error regarding the team color, I would like to know if im using a bad Event or just bad coding from my part.


local teamManager = BrickColor.new("Shamrock");

local seat = script.Parent; --The parent is a normal seat

seat.Changed:Connect(function(TeamCheck)
--seat.Touched:connect Works, but only once you leave the chair

	local plr = game.Players:GetPlayerFromCharacter(TeamCheck.Parent);

	local numberPlayers = 0
	for _,myplayer in pairs(game.Players:GetChildren()) do
		if myplayer.TeamColor.Name == "Shamrock" then
			numberPlayers = numberPlayers + 1
		end
	end

	if numberPlayers == 0 then
		plr.TeamColor = teamManager
	end

end)

If someone understand why this is happening I would be very grateful, thank you for you time and help!

Make a conditional statement that ensures plr actually exists. Since this event signal will also fire if a player leaves the seat, this results in TeamCheck = nil and thus makes plr = nil because of the function you are using.

Also, use Instance:GetPropertyChangedSignal (roblox.com) over Instance:Changed. Reason being is you can narrow down which property you are trying to target.

1 Like

There is a good example of using seat occupant, maybe use that instead:
https://developer.roblox.com/en-us/api-reference/class/Seat

relevant exerpt:

seat:GetPropertyChangedSignal("Occupant"):Connect(function()
    	local humanoid = seat.Occupant 
    	if humanoid then...
1 Like

How may I use since the .parent is no longer working?
“plr = game.Players:GetPlayerFromCharacter(TeamCheck.parent)”

It would work if you did this before that line:

seat:GetPropertyChangedSignal("Occupant"):Connect(function()
    	local TeamCheck = seat.Occupant 
    	if TeamCheck then ....
1 Like