How to get player's name and force sit them

I’m trying to make a game that, when it starts, puts players in seats.

The problem I’m facing is it’s pretty hard to get the players and put them in the seats. So far I have this method:

local player = game.Players.LocalPlayer.Name

game.Workspace.TableArea.Chairs.Chair1.Seat:Sit(game.Workspace.player)

From what I’ve done so far, I’ve found this to not work and be pretty inefficient. All help would be appreciated!

The :Sit() dont exist
Make like this

local PlayerHumanoid = game.Workspace[game.Players.LocalPlayer.Name].Humanoid

PlayerHumanoid.Sit = true

ServerScriptService.Script:1: attempt to index nil with ‘Name’ - Server - Script:1

I believe I got this because it’s a serverside script

local playerHumanoid = game.Players["Name"].Humanoid

playerHumanoid.Sit = true

oh, u cant get the game.Players.LocalPlayer in scripts just local scripts

game.Players.PlayerAdded:Connect(function(Player)
    local PlayerName = Instance.new("StringValue")
    PlayerName.Parent = game.ReplicatedStorage
    PlayerName.Name = "PlayerName"
    PlayerName.Value = Player.Name
end)

game.Workspace[game.ReplicatedStorage.PlayerName.Value].Humanoid.Sit = true

Are you trying to make all players sit on specific seats? What you can do is put al lthe seats in a folder and do a loop for every seat there and if the sit is not occupied, make a player sit there and remove from from the list

local players = {}

for i,v in pairs(game.Players:GetPlayers()) do
    table.insert(players,v.Name) -- insert player names to table
end


for _,seat in ipairs(workspace.TableArea.Chairs:GetChildren()) do
   if seat:IsA("Seat") and not seat.Occupant then -- if the seat is a seat and doesn't have anyone on it
      local playerchosen = math.random(1,#players)
      local chosenchar = workspace:FindFirstChild(playerchosen)
      seat:Sit(chosenchar.Humanoid)
      table.remove(players,playerchosen)
   end
end

This code will loop through all the chairs and if they don’t have anyone on them (checked by seat.Occupant, which if there’s someone on it, it’s set to the humanoid of whoever is sitting on it), it makes a local varaible that gets a random number between 1 and how many players are in game, then it makes a person in the players table sit there, then removes from the table so they can’t be chosen them. Hopefully this works for how you wanted it since I haven’t tested it out myself, but it should,I think

What would belong in the players variable?

The variable at first is empty, but then contains every player that is in the game currently, and when a player form that list is seated, their name is removed from the table, since it contains the names of the players ingame. This code will probably need to be in an event since this will only run once, so perhaps make a bindableevent (correct me if i’m wrong), that you can fire everytime you want a game to start

“ServerScriptService.Script:10: invalid argument #2 to ‘random’ (interval is empty) - Server - Script:10”

Whelp

Again like I said this code will as soon as the server is made, so it’ll get the players immediately, you’d have to put it somewhere in an event that only runs that code when you tell it to run, cause when the server is ran, there’s no players at the start so the table will stay empty and the reason why that line errored

Alright, I’ll give that a go. I’ll get back to ya when I test it!

Alright, I’m not sure how you can make an Event that you can run yourself whenever, but it’s definitely not a RemoteEvent if you were thinking that, it’s probably a BindableEvent

Looks like we have a new error!

ServerScriptService.Script:14: attempt to index nil with ‘Humanoid’

Oh wait I see what’s wrong, I forgot make it reference the player in the table with the number,

replace that line with

local chosenchar = workspace:FindFirstChild(players[playerchosen])

playerchosen only contains the number, I didn’t make it get the name of the player in that index

Ayy it works! I’ve marked your answer as the solution, thanks for your help!

Anytime! If you have any more issues dont be afraid to make another post!

1 Like