Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

attempt to get length of a number value?

So basically I'm making a teleport system that spawns players In each seat and I'm getting an error from a line.

local randSeatAvail = math.floor(math.random(1, #seatAvail)) -- 3
local chosenSeat = seatAvail[randSeatAvail] -- seatAvail[1]

Full:

local Seats = workspace.Seats
local seatList = {}
local seatAvail = {}

local Players = game:GetService("Players")

local RSModule = game:GetService("ReplicatedStorage").PublicGameModules

--- Modules

local EventTable = require(RSModule.Events)
local Sounds = require(RSModule.Popup)

function ResetSeatList()
	print("resetseat activated")
	for i = 1, #seatList do
		print("for loop no." .. i)
		seatList[i] = i
	end

end

function StartGame()
	
	
	for i, v in pairs(Seats:GetChildren()) do
		seatAvail = i
		seatList = v.Position
		print(seatAvail, seatList)
	end


	for _, player in ipairs(Players:GetPlayers()) do
		local randSeatAvail = math.floor(math.random(1, #seatAvail)) -- 3
		local chosenSeat = seatAvail[randSeatAvail] -- seatAvail[1]
		player.HumanoidRootPart.CFrame = seatList[chosenSeat] -- seatList[3]
		table.remove(seatAvail, randSeatAvail)   
	end
	
end


EventTable.StartGame.OnServerEvent:Connect(StartGame)

1 answer

Edit: Code:

local Seats = workspace.Seats
local seatList = {}
--local seatAvail = {} -- Not needed!

local Players = game:GetService("Players")


-- commented out for me to debug since i don't have these modules!
--local RSModule = game:GetService("ReplicatedStorage").PublicGameModules

--- Modules

--local EventTable = require(RSModule.Events)
--local Sounds = require(RSModule.Popup)

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) -- 3

			local chosenSeat = seatList[randSeatAvail] -- seatAvail[1]
			
			local character = player.Character
			if not character or not character.Parent then
				character = player.CharacterAdded:wait()
				wait(1) -- needed to wait for the character to actually load before moving it...
			end
	
			character.HumanoidRootPart.Position = chosenSeat -- seatList[3]
			table.remove(seatList, randSeatAvail)
		end
    end

end
--\/\/\/\/ Hax!! 
Players.PlayerAdded:connect(function(plr)
	StartGame()
end)

--// start game from event #StartGame
--EventTable.StartGame.OnServerEvent:Connect(StartGame)

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) -- needed to wait for the character to actually load before moving it...
	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")

-- commented out for me to debug since i don't have these modules!
--local RSModule = game:GetService("ReplicatedStorage").PublicGameModules

--- Modules

--local EventTable = require(RSModule.Events)
--local Sounds = require(RSModule.Popup)

function ResetSeatList()
   -- print("resetseat activated")
	for i,Seat in pairs(Seats:GetChildren()) do
		--print("for loop no." .. i)
		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) -- seatList[3]
		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) -- 3

		local chosenSeat = seatList[randSeatAvail] -- seatAvail[1]

		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

0

Now I am getting: attempt to get length of a Instance value

0

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?

This discussion is preserved as it was. New answers, comments, and votes are closed.