Can anyone help with this code, I’m not that good with coding.
I’m not that good at code either, and I don’t really have a solution for this, but @CapnFry has a good tutorial for this so you may as well check it out.
Replace the remove the end
in your PlayerAdded event, and add an end
to finish off the gameStart
function
Here’s a fixed version of your code, with some comments to help explain what’s changed.
local function gameStart() -- always make your functions local, unless they *need* to be global
local n = math.random(2) -- omitting the 2nd parameter is equivalent to "math.random(1, n)"
local selectedMap = "Map" .. n
print("The map is:", selectedMap)
wait(1)
print("Teleporting")
local map = game.ReplicatedStorage.Maps[selectedMap]:Clone() -- indexing using square brackets is the same as dot index, but allows us to use variables
map.Parent = workspace -- workspace, Workspace, and game.Workspace are all valid ways to index the workspace (capitalisation matters, game.workspace is deprecated)
if selectedMap == "Map1" then
-- using a pairs loop is better for readability, and you should use :GetPlayers() instead of :GetChildren()
-- when trying to get a table of all the players in the game
for _,player in pairs(game.Players:GetPlayers()) do
player.Character:MoveTo(Vector3.new(36.75, 16.499, 9.65))
end
elseif selectedMap == "Map2" then -- you should use an elseif statement here instead of 2 separate if statements
for _,player in pairs(game.Players:GetPlayers()) do
player.Character:MoveTo(Vector3.new(164.35, 72.299, 115.75))
end
end
end
game.Players.PlayerAdded:Connect(function()
wait(3)
gameStart()
end)
I also think you need to fix your code’s logic on the PlayerAdded function: whenever a player joins, 3 seconds later a new map will be created and everyone gets teleported, what’s stopping someone from repeatedly rejoining and ruining the game for everyone? A new game should be started only when the previous game ends.
This might be late, but with the code logic, it seems to work, unless multiple players join at once, I’ll fix it eventually.