Edit:
Code:
local Seats = workspace.Seats
local seatList = {}
local Players = game:GetService("Players")
function ResetSeatList()
print("resetseat activated")
for i = 1, Seats:GetChildren() do
print("for loop no." .. i)
seatList[i] = Seats[i]
end
end
function StartGame()
print("Starting!!!...")
for i, v in pairs(Seats:GetChildren()) do
seatList[i] = v.Position
end
for _, player in ipairs(Players:GetChildren()) do
if(#seatList > 0) then
local randSeatAvail = math.random(1, #seatList)
local chosenSeat = seatList[randSeatAvail]
local character = player.Character
if not character or not character.Parent then
character = player.CharacterAdded:wait()
wait(1)
end
character.HumanoidRootPart.Position = chosenSeat
table.remove(seatList, randSeatAvail)
end
end
end
Players.PlayerAdded:connect(function(plr)
StartGame()
end)
just uncomment bits you need and remove the Players.PlayerAdded event in this script since you're handling the players through your remote event.
Not great modification but it should help you with coding this type of system.
The bit:
local character = player.Character
if not character or not character.Parent then
character = player.CharacterAdded:wait()
wait(1)
end
This is rather slow and hacky, but it works as an example. You would need to hook up each player to their seat position on load.
A much better way to make this work would be something like:
local Seats = workspace.Seats
local seatList = {}
local GamePlayers = {}
local GameEnd = false
local Players = game:GetService("Players")
function ResetSeatList()
for i,Seat in pairs(Seats:GetChildren()) do
if(Seat.Occupant == nil) then
seatList[#seatList+1] = Seat.Position
end
end
end
function GameLoop()
for player,SeatTble in pairs(GamePlayers) do
local character = player.Character
local chosenSeat = SeatTble.mySeat
if(SeatTble.isSitting == false) then
SeatTble.isSitting = true
character.HumanoidRootPart.CFrame = CFrame.new(chosenSeat)
end
end
wait()
if(GameEnd == false) then
GameLoop()
end
end
Players.PlayerAdded:connect(function(plr)
if(#seatList > 0) then
local randSeatAvail = math.random(1, #seatList)
local chosenSeat = seatList[randSeatAvail]
local character = plr.Character
if not character or not character.Parent then
character = plr.CharacterAdded:wait()
end
GamePlayers[plr] = {
mySeat = chosenSeat,
isSitting = false
}
table.remove(seatList, randSeatAvail)
end
end)
Players.PlayerRemoving:connect(function(plr)
if(GamePlayers[plr] ~= nil) then
GamePlayers[plr] = nil
end
ResetSeatList()
end)
The above example will need to keep track on who jumps up away from their seat and re add the seat to the global seatList Table for it to work flawlessly.
Just an idea on how to do this.
Might need to change a lot of it to get it to work with your events and modules though.
hope this helps!
2 comments
Now I am getting: attempt to get length of a Instance value
Ill see what i can do. Was tired when i left this answer. So seatAvail and seatList are both tables right?, seatAvail holds the Index of where the seat is? and seatList is the list of the physical seat blocks right?