Hello, I’ve ran into an issue where I cannot get a script to where multiple teams can sit on a seat whereof any unwritten teams cannot sit on the seat.
I had a script but it is incredibly bad and I can’t think of any solutions. If you can help, I would greatly appreciate it. Thank you.
I’m not talking about CollisionGroups. By that I mean I mean the service that ROBLOX provides under Explorer and I want this to be done in a specific seat that can be replicated. My main goal is to just get a vehicle seat that kicks out a player if they’re not in a defined team. Thank you.
I think this aligns with the behavior you’re looking for; just make sure to replace the teams within the table.
local Players = game:GetService("Players")
local Teams = game:GetService("Teams")
local Seat = script.Parent
local TeamsAllowed = {
[Teams.Blue] = true,
[Teams.Green] = true
}
Seat:GetPropertyChangedSignal("Occupant"):Connect(function()
local Occupant = Seat.Occupant
if not Occupant then return end -- Player is standing up
local Player = Players:GetPlayerFromCharacter(Occupant.Parent)
if not TeamsAllowed[Player.Team] then
wait(0.5) -- Short delay to ensure that the player is removed
Occupant.Sit = false
return -- This player is not allowed to sit
end
end)
Colbert explained how this solution is flawed. However, if you want to see it you can look at the the old solution below.
Old Solution
I would just break the weld instantly, rather than doing that.
local Players = game:GetService("Players")
local Teams = game:GetService("Teams")
local Seat = script.Parent -- put this inside a seat
local TeamsAllowed = {
[Teams.Blue] = true,
[Teams.Green] = true
}
Seat:GetPropertyChangedSignal("Occupant"):Connect(function()
local Occupant = Seat.Occupant
if not Occupant then return end -- if the occupant was set from it existing to it being nil then this shouldn't run
local Player = Players:GetPlayerFromCharacter(Occupant.Parent)
if not TeamsAllowed[Player.Team] and Seat:FindFirstChild("SeatWeld") then
-- there's no need for a delay because this is instant
Seat:FindFirstChild("SeatWeld"):Destroy() -- destroy it because this is way cooler
return -- end it here just incase there's any code after this
end
end)
Breaking the seat weld isn’t enough because the Humanoid is still registered as the sitting Occupant of the seat. In addition to this, instantly destroying the weld can result in a race condition where either you receive an error of “Something unexpectedly tried to set the Parent of SeatWeld” or the code runs ahead of time before the SeatWeld is created.
This disables the Humanoid’s ability to sit on any seat after they’ve sat on the seat in question. It works for the purposes of preventing sitting at all but it doesn’t work for what OP is looking for which is granular control over what seats a Humanoid is allowed to sit in.
Did you mean to use the ChangeState function to get the Humanoid out of the sitting state? This works as well, however it’s no different from Spooce’s solution (Sit = false) and using this immediately after a player sits will not move them out of the seat.