How can I make it so players spawn in and all the excess plates with a blank ‘Owner’ value are deleted? I’m not the most familiar with for i, v in pairs stuff, so if anyone can help me out here it will be appreciated!
basically I need players teleporting to a random plate upon round start and excess plot deleting implemented in this script
local rep = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")
local events = 5
chosenEvent = ""
local ce = chosenEvent
local plateTemplate = game.ServerStorage.Player_Baseplates
local values = rep.Values
local current = values.CurrentEvent
local eventtype = current.EventType
local highlight = current.ObjectToHighlight
local status = values.Status
local inRound = false
local inGame = workspace.Ingame
local reqPlayers = 2
local function chooseEvent()
chosenEvent = math.random(1, events)
ce = chosenEvent
end
local function executeEvent()
if ce == 1 then
eventtype.Value = "Player"
current.Value = "A player will get +10 jump power"
status.Value = current.Value
elseif ce == 2 then
eventtype.Value = "Player"
current.Value = "A player will become sparkly"
status.Value = current.Value
elseif ce == 3 then
eventtype.Value = "Arena"
current.Value = "Arena: Fire will fall from the sky"
status.Value = current.Value
elseif ce == 4 then
eventtype.Value = "Plate"
current.Value = "Someone's plate will light on fire"
status.Value = current.Value
elseif ce == 5 then
eventtype.Value = "Plate"
current.Value = "Someone's plate will flip upside-down"
status.Value = current.Value
end
end
while task.wait() do
if #players:GetChildren() >= reqPlayers then
wait(1)
for i = 1,15 do
status.Value = "Intermission: "..15-i
task.wait(1)
end
status.Value = "Teleporting players..."
--Here
else
status.Value = "Waiting for minimum of 2 players."
end
end
Make a table of plates and use a for i, v in pairs to loop through a model or folder where all of the plates are held.
how it works
local plates = {} - a table of the plates
for index, variable in pairs(platesFolder:GetChildren()) do -- each child of the plates folder will be considered a variable, while the index will be the platesFolder itself.
table.insert(plates, variable))
end
then after you can assign each player to a random plate like so.
for i, v in pairs(game.Players:GetPlayers()) do
player.Character.PrimaryPart.CFrame = plates[math.random(1, #plates)].CFrame
end
Personally, I would create the plates for each player individually, instead of creating 64 at once and then deleting them after. It depends on how you set up your plates though, a picture of your explorer would help.
Your script doesn’t even teleport the players yet, that’s why we need more info before we help you.
I drafted some code for you, but it may not work due to the few details I had to work with:
--//Services
local ServerStorage = game:GetService("ServerStorage")
local rep = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")
--//Variables
local plateTemplate = ServerStorage.Player_Baseplates
local values = rep.Values
local current = values.CurrentEvent
local eventtype = current.EventType
local highlight = current.ObjectToHighlight
local status = values.Status
local currentPlates = workspace.Player_Baseplates
local inGame = workspace.Ingame
--//Controls
local RNG = Random.new()
local events = 5
local chosenEvent = ""
local ce = chosenEvent
local inRound = false
local reqPlayers = 2
--//Tables
local plateOwners = {}
--//Functions
local function chooseEvent()
chosenEvent = math.random(1, events)
ce = chosenEvent
end
local function executeEvent()
if ce == 1 then
eventtype.Value = "Player"
current.Value = "A player will get +10 jump power"
elseif ce == 2 then
eventtype.Value = "Player"
current.Value = "A player will become sparkly"
elseif ce == 3 then
eventtype.Value = "Arena"
current.Value = "Arena: Fire will fall from the sky"
elseif ce == 4 then
eventtype.Value = "Plate"
current.Value = "Someone's plate will light on fire"
elseif ce == 5 then
eventtype.Value = "Plate"
current.Value = "Someone's plate will flip upside-down"
end
status.Value = current.Value
end
while true do
if #players:GetPlayers() >= reqPlayers then
task.wait(1)
for i = 1, 15 do
status.Value = "Intermission: ".. 15 - i
task.wait(1)
end
if #players:GetPlayers() < reqPlayers then
continue
end
status.Value = "Teleporting players..."
--//Create all plates
currentPlates:ClearAllChildren()
plateTemplate:Clone().Parent = currentPlates
--//Clear plateOwners table
table.clear(plateOwners)
--//Teleport players to unowned plates
for i, player in ipairs(players:GetPlayers()) do
local randomPlate = currentPlates:GetChildren()[RNG:NextInteger(1, #currentPlates:GetChildren())]
while plateOwners[randomPlate] do
randomPlate = currentPlates:GetChildren()[RNG:NextInteger(1, #currentPlates:GetChildren())]
end
plateOwners[randomPlate] = player
player:MoveTo(randomPlate.Position)
end
--//Destroy unowned plates
for i, plate in ipairs(currentPlates) do
if not plateOwners[plate] then
plate:Destroy()
end
end
else
status.Value = "Waiting for minimum of 2 players."
players.PlayerAdded:Wait()
end
end
Position is not a valid member of Folder "Workspace.Player_Baseplates.Player_Baseplates" - Main:82
Stack Begin
Script 'ServerScriptService.Main', Line 82 - Main:82
Stack End
I believe I have fixed it, but also, is there a value in each plate called “Owner”? And if so, what type of value is it?
Code:
--//Services
local ServerStorage = game:GetService("ServerStorage")
local rep = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")
--//Variables
local plateTemplate = ServerStorage.Player_Baseplates
local values = rep.Values
local current = values.CurrentEvent
local eventtype = current.EventType
local highlight = current.ObjectToHighlight
local status = values.Status
local currentPlates = workspace.Player_Baseplates
local inGame = workspace.Ingame
--//Controls
local RNG = Random.new()
local events = 5
local chosenEvent = ""
local ce = chosenEvent
local inRound = false
local reqPlayers = 2
--//Tables
local plateOwners = {}
--//Functions
local function chooseEvent()
chosenEvent = RNG:NextInteger(1, events)
ce = chosenEvent
end
local function executeEvent()
if ce == 1 then
eventtype.Value = "Player"
current.Value = "A player will get +10 jump power"
elseif ce == 2 then
eventtype.Value = "Player"
current.Value = "A player will become sparkly"
elseif ce == 3 then
eventtype.Value = "Arena"
current.Value = "Arena: Fire will fall from the sky"
elseif ce == 4 then
eventtype.Value = "Plate"
current.Value = "Someone's plate will light on fire"
elseif ce == 5 then
eventtype.Value = "Plate"
current.Value = "Someone's plate will flip upside-down"
end
status.Value = current.Value
end
while true do
if #players:GetPlayers() >= reqPlayers then
task.wait(1)
for i = 1, 15 do
status.Value = "Intermission: ".. 15 - i
task.wait(1)
end
if #players:GetPlayers() < reqPlayers then
continue
end
status.Value = "Teleporting players..."
--//Clear plateOwners table
table.clear(plateOwners)
--//Create all plates
currentPlates:ClearAllChildren()
for plate in ipairs(plateTemplate:GetChildren()) do
plate:Clone().Parent = currentPlates
end
--//Teleport players to unowned plates
for i, player in ipairs(players:GetPlayers()) do
local randomPlate = currentPlates:GetChildren()[RNG:NextInteger(1, #currentPlates:GetChildren())]
while plateOwners[randomPlate] do
randomPlate = currentPlates:GetChildren()[RNG:NextInteger(1, #currentPlates:GetChildren())]
end
plateOwners[randomPlate] = player
player:MoveTo(randomPlate.Position)
end
--//Destroy unowned plates
for i, plate in ipairs(currentPlates) do
if not plateOwners[plate] then
plate:Destroy()
end
end
--//Main game
else
status.Value = "Waiting for minimum of 2 players."
players.PlayerAdded:Wait()
end
end