Need help with map spawner script

local oof = {}
oof[1] = game.Workspace.part1
oof[2] = game.Workspace.part2
oof[3] = game.Workspace.part3
oof[4] = game.workspace.part4

game.Workspace.part1.Position = game.Workspace.part1.Position - Vector3.new(0,10,0)
game.Workspace.part2.Position = game.Workspace.part2.Position - Vector3.new(0,10,0)
game.Workspace.part3.Position = game.Workspace.part3.Position - Vector3.new(0,10,0)
game.Workspace.part4.Position = game.Workspace.part4.Position - Vector3.new(0,10,0)
game.Workspace.part1.CanCollide = false
game.Workspace.part2.CanCollide = false
game.Workspace.part3.CanCollide = false
game.Workspace.part4.CanCollide = false
game.Workspace.part1.Transparency = 1
game.Workspace.part2.Transparency = 1
game.Workspace.part3.Transparency = 1
game.Workspace.part4.Transparency = 1
game.Players.PlayerAdded:Connect(function()
	local mapSelect = oof[math.random(1,4)]
	if mapSelect == 1 then
		game.Workspace.part1.Transparency = 1
		game.Workspace.part1.CanCollide = true
		game.Workspace.part1.Position = game.Workspace.part1.Position + Vector3.new(0,10,0)
	end
	if mapSelect == 2 then
		game.Workspace.part2.Transparency = 1
		game.Workspace.part2.CanCollide = true
		game.Workspace.part2.Position = game.Workspace.part2.Position + Vector3.new(0,10,0)
	end
	if mapSelect == 3 then
		game.Workspace.part3.Transparency = 1
		game.Workspace.part3.CanCollide = true
		game.Workspace.part3.Position = game.Workspace.part3.Position + Vector3.new(0,10,0)
	end
	if mapSelect == 4 then
		game.Workspace.part4.Transparency = 1
		game.Workspace.part4.CanCollide = true
		game.Workspace.part4.Position = game.Workspace.part4.Position + Vector3.new(0,10,0)
	end
end)

This doesnt work but doesnt have any errors apparently

Because You turn it into nil ;-;

This You should do

local mapSelect = math.random(1,#oof)

I believe this is not how you insert Items in to a table
use table.insert or make the table with the values.

local oof = {
game.Workspace.part1,
game.Workspace.part2,
game.Workspace.part3,
game.workspace.part4
}

Table Wiki:

As another suggestion, iterate the table with the map selected.

local mapSelect = oof[math.random(1,#oof)]
mapSelect.Transparency = 1
mapSelect.CanCollide = true
mapSelect.Position = Position

and you can Resize your code by

game.Players.PlayerAdded:Connect(function()
	local mapSelect = math.random(1,#oof)
    game.Workspace.part[mapSelect].Transparency = 1
game.Workspace.part[mapSelect].CanCollide = true
game.Workspace.part[mapSelect].Position = game.Workspace.part[mapSelect].Position+ Vector3.new(0,10,0)
end)
2 Likes

I’ve rewritten your script to be much more organized, along with a potential fix for your problem:

local oof = {}

for i = 1, 4 do
    local part = workspace:FindFirstChild("part"..i)
    oof[i] = part
    part.Position = part.Position-Vector3.new(0, 10, 0)
    part.CanCollide = false
    part.Transparency = 1
end

game.Players.PlayerAdded:connect(function(player)
    local part = oof[math.random(1, #oof --[[#oof would be more efficient than 4]])]
    part.Transparency = 1
    part.CanCollide = true
    part.Position = part.Position+Vector3.new(0, 10, 0)
end)

The problem was, you set the mapSelect variable to a random value in the oof table. The table consisted of BasePart objects, whereas the ifs were checking for numbers.