Map changer script doesn't work

Hello,

I’m currently working on a game. The game include’s this script (map changer script). But sometimes if I play/test the game the script doesn’t work and it shows this error message in the output. If anyone knows how I fix this please let me know!

ServerStorage = game:GetService("ServerStorage")
ReplicatedStorage = game:GetService("ReplicatedStorage")
Players = game:GetService("Players")

Maps = ServerStorage:WaitForChild('Maps'):GetChildren()
Status = ReplicatedStorage:WaitForChild('Status')

while true do
	
	wait()

	local ChosenMap = Maps[math.random(1, #Maps)]:Clone()
	ChosenMap.Parent = workspace
	local Spawns = ChosenMap:FindFirstChild('Spawns'):GetChildren()


	Status.Value = 'Choosing Map.'
	wait(0.4)
	Status.Value = 'Choosing Map..'
	wait(0.4)
	Status.Value = 'Choosing Map...'
	wait(0.4)
	Status.Value = 'Choosing Map.'
	wait(0.4)
	Status.Value = 'Choosing Map..'
	wait(0.4)

	Status.Value = 'Map chosen.'
	
	local RandomSpawn = Spawns[math.random(0, #Spawns)]
	
	wait(2)
	
	for _, Player in pairs(Players:GetChildren())do
		if Player.Character and Player.Character:FindFirstChild('Humanoid') then
			Player.Character.HumanoidRootPart.CFrame = RandomSpawn.CFrame
		end
	end
	
	Countdown = 300 -- Game Time

	repeat wait(1)
		Countdown = Countdown - 1

		Status.Value = 'Time Left: '..Countdown
	until Countdown <= 0
	
	local replicatedStorage = game:GetService("ReplicatedStorage")
	local removeEvent = replicatedStorage:WaitForChild("LoadingScreen")

	removeEvent:FireAllClients()


	ChosenMap:Destroy()
	wait()
	
	Status.Value = 'Round Ended, waiting for new game.'
	
	wait(2)
end

image

2 Likes

Try doing
Player.Character.HumanoidRootPart.CFrame = RandomSpawn.Position
or
Player.Character.HumanoidRootPart.CFrame = RandomSpawn.CFrame + Vector3.new(0,4,0)

That won’t work because it’s a nil value according to the error.


To OP:
Lua “arrays” (tables) are 1-based, not 0-based so change it to 1 in the math.random part: Maps[math.random(1, #Maps)]:Clone()

2 Likes

In lua tables start at index 1, index 0 would be a nil value. Change this in your randomSpawn selector.

Try do this

Player.Character.HumanoidRootPart.CFrame = RandomSpawn.CFrame + Vector3.new(0,5,0)

Also fix this

local RandomSpawn = Spawns[math.random(1,#Spawns)]

Try this
Player.Character:SetPrimaryPartCFrame(RandomSpawn.CFrame)

I think I misunderstood the error, my bad.

try this

ServerStorage = game:GetService("ServerStorage")
ReplicatedStorage = game:GetService("ReplicatedStorage")
Players = game:GetService("Players")

Maps = ServerStorage:WaitForChild('Maps'):GetChildren()
Status = ReplicatedStorage:WaitForChild('Status')

while true do
	
	wait()

	local ChosenMap = Maps[math.random(1, #Maps)]:Clone()
	ChosenMap.Parent = workspace
	local Spawns = ChosenMap:FindFirstChild('Spawns'):GetChildren()


	Status.Value = 'Choosing Map.'
	wait(0.4)
	Status.Value = 'Choosing Map..'
	wait(0.4)
	Status.Value = 'Choosing Map...'
	wait(0.4)
	Status.Value = 'Choosing Map.'
	wait(0.4)
	Status.Value = 'Choosing Map..'
	wait(0.4)

	Status.Value = 'Map chosen.'
	
	local RandomSpawn = Spawns[math.random(1, #Spawns)]
	
	wait(2)
	
	for i,v in pairs(game.Workspace:GetChildren()) do
if v:IsA("Model") then
if v:FindFirstChild("HumanoidRootPart") then
v.HumanoidRootPart.CFrame = RandomSpawn.CFrame
end
end
end
	
	Countdown = 300 -- Game Time

	repeat wait(1)
		Countdown = Countdown - 1

		Status.Value = 'Time Left: '..Countdown
	until Countdown <= 0
	
	local replicatedStorage = game:GetService("ReplicatedStorage")
	local removeEvent = replicatedStorage:WaitForChild("LoadingScreen")

	removeEvent:FireAllClients()


	ChosenMap:Destroy()
	wait()
	
	Status.Value = 'Round Ended, waiting for new game.'
	
	wait(2)
end
1 Like