This is a script from a game teleporter and I wanted it so if a play is sitting in a seat inside the teleport model, it will teleport everyone in that seat to the specified placeID. How do I check if the player is sitting in the chair?
Script:
script.Parent.Gate.Touched:Connect(function(hit)
if hit.Parent:findFirstChild("Humanoid") then
if teleporting == false then
local char = hit.Parent
local player = game.Players:FindFirstChild(char.Name)
local alreadyExists = false
for i=1,#list do
if list[i] == char.Name then
alreadyExists = true
end
end
if alreadyExists == false then
if #list < 3 then -- Many Players have been teleported.
table.insert(list,char.Name)
char.PrimaryPart.CFrame = spawnTeleport.CFrame
updateGui()
leaveGuiEvent:FireClient(player)
end
player.CharacterRemoving:connect(function(character)
removeFromList(character)
end)
end
end
end
end)
if seat.Occupant then
local plr = game.Players:GetPlayerFromCharacter(seat.Occupant)
if plr then
if teleporting == false then
local char = plr.Character
local player = game.Players:FindFirstChild(char.Name)
local alreadyExists = false
for i=1,#list do
if list[i] == char.Name then
alreadyExists = true
end
end
if alreadyExists == false then
if #list < 3 then -- Many Players have been teleported.
table.insert(list,char.Name)
char.PrimaryPart.CFrame = spawnTeleport.CFrame
updateGui()
leaveGuiEvent:FireClient(player)
end
player.CharacterRemoving:connect(function(character)
removeFromList(character)
end)
end
end
end
end
Not sure why you have a whole new variable for plr called “player”. But, by the looks of it, it should work. (I would just remove the variable “player” and replace all references to it as “plr”)
To check if a player is seated you can use the seated event, for example: Humanoid.Seated:Connect(function()
This should be placed in a LocalScript, so you can get the LocalPlayer
Here an article explaining more.
That makes sense. A little problem is that I want to get everyone that is sitting in seats that are located in a specific model to be teleported all at once. So I’m not sure how I can do that.
if you want to know if a player is sitting you just do game.Players:FinadFisrtChild(player).character.Humanoid.Sit if it is true, the players is sitting
Use a for i,v loop with GetDescendants() on the entire workspace. Use an if statement to check if the object is a seat, if it is, check if the Occupant property value is the specific player you are looking for. If you don’t know how to do this lmk and I can provide a code example.
local playersitting = false
local seat = script.parent.seat --you can change this to your seat
--detect if players sits in seat
seat.ChildAdded:Connect(function(child)
if child.Name == "SeatWeld" then
local character = child.Part1.Parent
local player = game.Players.GetPlayerFromCharacter(character)
playersitting = true
end)
--detect if players gets up from seat
seat.ChildRemoved:Connect(function(child)
if child.Name == "SeatWeld" then
local character = child.Part1.Parent
local player = game.Players.GetPlayerFromCharacter(character)
playersitting = false
end)
seat.Occupant is always the humanoid of a char, not the char itself. instead use:
if seat.Occupant then
local plr = game.Players:GetPlayerFromCharacter(seat.Occupant.Parent)
if plr then
if teleporting == false then
local char = plr.Character
local player = game.Players:FindFirstChild(char.Name)
local alreadyExists = false
for i=1,#list do
if list[i] == char.Name then
alreadyExists = true
end
end
if alreadyExists == false then
if #list < 3 then -- Many Players have been teleported.
table.insert(list,char.Name)
char.PrimaryPart.CFrame = spawnTeleport.CFrame
updateGui()
leaveGuiEvent:FireClient(player)
end
player.CharacterRemoving:connect(function(character)
removeFromList(character)
end)
end
end
end