Minigame doesn't teleport bug: attempt to index nil with 'TeleportLocation'" on line 156

Hello, I am a new scripter trying to make a obby minigame where the game chooses a random map out of 5, displays a 15-second counting down animation, then loads the map and teleports you to a pad called “TeleportLocation” which has you complete a obby within 30 seconds. After beating it, it shows what place you got and you get rewarded with 50 coins, lowering by 5 for second place, and so on. You also get a win that saves to a database. Then it destroys the map, teleports you back to the lobby, and picks a different one after another intermission.

But for some reason there’s no countdown, and I get a error. Even though the map gets picked and loads in, I don’t get teleported nor does the countdown start.
I also have a error called “attempt to index nil with ‘TeleportLocation’” on line 156
Which shouldn’t appear because I cloned a map model with TeleportLocation, tested it, and TeleportLocation is still here, with the map loaded in, still not working nor teleporting. It has 0 warning or error until I run the game but I can still provide my code incase I did something wrong, but if it’s wrong, then the map shouldn’t even load in.

Am I missing something?

Why is it not working?

Heres my code with comments explaining everything incase I messed up or am missing something:

-- Function to load a map
function loadMap(mapName)
	-- Clone the specified map
	local newMap = game.ServerStorage[mapName]:Clone()
	newMap.Name = "Map"
	newMap.Parent = game.Workspace

	-- Return the new map
	return newMap
end


-- Load the first map
local mapInstance = loadMap("Map1")

-- Get the player
local player = game.Players.LocalPlayer

-- Set the map instance to nil
local mapInstance = nil

-- Constants
local timer = 30 -- Time limit for each map

-- Function to pick a random map from the ServerStorage model
local function pickRandomMap()
	local maps = {"Map1", "Map2", "Map3", "Map4", "Map5"}
	local index = math.random(1, #maps)
	return maps[index]
end

-- Function to show the intermission GUI
local function showIntermission(timeLeft)
	-- Clone the IntermissionGui
	local gui = script.Parent.IntermissionGui:Clone()
	gui.Parent = player:WaitForChild("PlayerGui")

	-- Slide the GUI in from the top
	local tweenInfo = TweenInfo.new(0.5, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)
	local position = UDim2.new(0.5, 0, 0, 0)
	local tween = game:GetService("TweenService"):Create(gui, tweenInfo, {Position = position})
	tween:Play()

	-- Wait for the specified time
	wait(timeLeft)

	-- Slide the GUI out to the top
	tween = game:GetService("TweenService"):Create(gui, tweenInfo, {Position = UDim2.new(0.5, 0, -1, 0)})
	tween:Play()

	-- Destroy the GUI
	gui:Destroy()
end


-- Function to end the game
local function endGame()
	-- Destroy the map
	local map = game.Workspace:FindFirstChild("Map")
	if map then
		map:Destroy()
	end

	-- Teleport the player back to the lobby
	local player = game.Players.LocalPlayer
	player.Character:MoveTo(game.Workspace.Lobby.TeleportLocation.Position)

	-- Show the intermission GUI
	showIntermission(15)
end



-- Function to show the player's placement
local function showPlacement(placement)
	-- Create the GUI
	local gui = script.Parent.PlacementGui:Clone()
	gui.Parent = script.Parent

	-- Set the placement text
	gui.PlacementText.Text = "You placed #" .. placement .. "!"

	-- Slide the GUI in from the right
	local tweenInfo = TweenInfo.new(0.5, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)
	local position = UDim2.new(1, 0, 0.5, 0)
	local tween = game:GetService("TweenService"):Create(gui, tweenInfo, {Position = position})
	tween:Play()

	-- Wait 5 seconds
	wait(5)

	-- Slide the GUI out to the right
	tween = game:GetService("TweenService"):Create(gui, tweenInfo, {Position = UDim2.new(2, 0, 0.5, 0)})
	tween:Play()

	-- Destroy the GUI
	gui:Destroy()
end

-- Function to start the game
local function startGame(player)
	-- Set the timer
	local timeLeft = timer
	script.Parent.TimerLabel.Text = "Time left: " .. timeLeft .. " seconds"

	-- Set the initial placement and reward
	local placement, reward = 0, 50

	-- Update the timer every second
	while timeLeft > 0 do
		timeLeft = timeLeft - 1
		wait(1)
		script.Parent.TimerLabel.Text = "Time left: " .. timeLeft .. " seconds"

		-- Check if the player has reached the finish line
		if player.Character:FindFirstChild("FinishLine") or player.Character:FindFirstChild("PlacementPart") then
			-- Increment the placement
			placement = placement + 1

			-- Award the coins
			game.ServerStorage.Coins:AwardCoins(player, reward)

			-- Show the placement
			showPlacement(placement)

			-- Decrement the reward
			reward = reward - 5

			-- End the game if there are no more rewards left
			if reward < 0 then
				break
			end
		end
	end

	-- End the game
	endGame()
end

-- Pick a random map and load it in when a player joins
game.Players.PlayerAdded:Connect(function(player)
	-- Pick a random map and load it in
	local map = pickRandomMap()
	local mapModel = game.ServerStorage:FindFirstChild(map)
	if mapModel then
		local mapInstance = mapModel:Clone()
		mapInstance.Name = "Map"
		mapInstance.Parent = game.Workspace
		if mapInstance and mapInstance:FindFirstChild("TeleportLocation") then
			player.Character:MoveTo(mapInstance.TeleportLocation.Position)
		end

	end

	-- Teleport the player to the map
	player.Character:MoveTo(mapInstance.TeleportLocation.Position)

	-- Start the game
	startGame(player)
end)

1 Like

What that error is trying to tell you is that TeleportLocation doesn’t exist or is nil (nil means non-existent, if a Instance is nil then it wasnt found or doesn’t exist)

Yeah, but when I run the game it does exist
image

1 Like

If it is giving you that error, it doesn’t exist or you didn’t give it the information to find it

Basically something is not letting it find it

1 Like

Could you please quote line 156 so we can better help you?

1 Like

I found the issue, you made mapInstance a local value

Try this:


-- Function to load a map
function loadMap(mapName)
	-- Clone the specified map
	local newMap = game.ServerStorage[mapName]:Clone()
	newMap.Name = "Map"
	newMap.Parent = game.Workspace

	-- Return the new map
	return newMap
end


-- Load the first map
local mapInstance = loadMap("Map1")

-- Get the player
local player = game.Players.LocalPlayer

-- Set the map instance to nil
local mapInstance = nil

-- Constants
local timer = 30 -- Time limit for each map

-- Function to pick a random map from the ServerStorage model
local function pickRandomMap()
	local maps = {"Map1", "Map2", "Map3", "Map4", "Map5"}
	local index = math.random(1, #maps)
	return maps[index]
end

-- Function to show the intermission GUI
local function showIntermission(timeLeft)
	-- Clone the IntermissionGui
	local gui = script.Parent.IntermissionGui:Clone()
	gui.Parent = player:WaitForChild("PlayerGui")

	-- Slide the GUI in from the top
	local tweenInfo = TweenInfo.new(0.5, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)
	local position = UDim2.new(0.5, 0, 0, 0)
	local tween = game:GetService("TweenService"):Create(gui, tweenInfo, {Position = position})
	tween:Play()

	-- Wait for the specified time
	wait(timeLeft)

	-- Slide the GUI out to the top
	tween = game:GetService("TweenService"):Create(gui, tweenInfo, {Position = UDim2.new(0.5, 0, -1, 0)})
	tween:Play()

	-- Destroy the GUI
	gui:Destroy()
end


-- Function to end the game
local function endGame()
	-- Destroy the map
	local map = game.Workspace:FindFirstChild("Map")
	if map then
		map:Destroy()
	end

	-- Teleport the player back to the lobby
	local player = game.Players.LocalPlayer
	player.Character:MoveTo(game.Workspace.Lobby.TeleportLocation.Position)

	-- Show the intermission GUI
	showIntermission(15)
end



-- Function to show the player's placement
local function showPlacement(placement)
	-- Create the GUI
	local gui = script.Parent.PlacementGui:Clone()
	gui.Parent = script.Parent

	-- Set the placement text
	gui.PlacementText.Text = "You placed #" .. placement .. "!"

	-- Slide the GUI in from the right
	local tweenInfo = TweenInfo.new(0.5, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)
	local position = UDim2.new(1, 0, 0.5, 0)
	local tween = game:GetService("TweenService"):Create(gui, tweenInfo, {Position = position})
	tween:Play()

	-- Wait 5 seconds
	wait(5)

	-- Slide the GUI out to the right
	tween = game:GetService("TweenService"):Create(gui, tweenInfo, {Position = UDim2.new(2, 0, 0.5, 0)})
	tween:Play()

	-- Destroy the GUI
	gui:Destroy()
end

-- Function to start the game
local function startGame(player)
	-- Set the timer
	local timeLeft = timer
	script.Parent.TimerLabel.Text = "Time left: " .. timeLeft .. " seconds"

	-- Set the initial placement and reward
	local placement, reward = 0, 50

	-- Update the timer every second
	while timeLeft > 0 do
		timeLeft = timeLeft - 1
		wait(1)
		script.Parent.TimerLabel.Text = "Time left: " .. timeLeft .. " seconds"

		-- Check if the player has reached the finish line
		if player.Character:FindFirstChild("FinishLine") or player.Character:FindFirstChild("PlacementPart") then
			-- Increment the placement
			placement = placement + 1

			-- Award the coins
			game.ServerStorage.Coins:AwardCoins(player, reward)

			-- Show the placement
			showPlacement(placement)

			-- Decrement the reward
			reward = reward - 5

			-- End the game if there are no more rewards left
			if reward < 0 then
				break
			end
		end
	end

	-- End the game
	endGame()
end

-- Pick a random map and load it in when a player joins
game.Players.PlayerAdded:Connect(function(player)
	-- Pick a random map and load it in
	local map = pickRandomMap()
	local mapModel = game.ServerStorage:FindFirstChild(map)
	if mapModel then
	 mapInstance = mapModel:Clone()
		mapInstance.Name = "Map"
		mapInstance.Parent = game.Workspace
		if mapInstance and mapInstance:FindFirstChild("TeleportLocation") then
			player.Character:MoveTo(mapInstance.TeleportLocation.Position)
		end

	end

	-- Teleport the player to the map
	player.Character:MoveTo(mapInstance.TeleportLocation.Position)

	-- Start the game
	startGame(player)
end)

1 Like

I quoted it now, it should be easier to see whats wrong

2 Likes

mapInstance was a local value so after the function ended the value was discarded @littlenoobcafe
Fix

2 Likes

Your code could cause issues because it only declares the variable if the mapModel actually exists

1 Like

The only thing I changed was the value thing, that code was already there

1 Like

The line should be inside of the if statement because thats where your mapInstance variable is declared:

game.Players.PlayerAdded:Connect(function(player)
	-- Pick a random map and load it in
	local map = pickRandomMap()
	local mapModel = game.ServerStorage:FindFirstChild(map)
	if mapModel then
		local mapInstance = mapModel:Clone()
		mapInstance.Name = "Map"
		mapInstance.Parent = game.Workspace
		if mapInstance and mapInstance:FindFirstChild("TeleportLocation") then
			player.Character:MoveTo(mapInstance.TeleportLocation.Position)
		end
	end

	-- Start the game
	startGame(player)
end)

Sorry for the bad formatting im on a phone

1 Like

It stays local for better performance, and overall its only used in the if statement scope so that doesnt matter

2 Likes

I’ll try that. Unfortunately I can’t try it right now but I’ll try as soon as I wake up

1 Like

It seems that there are no errors now, but I still don’t get teleported
I changed the code around, here’s the new code:

-- Function to load a map
function loadMap(mapName)
	-- Clone the specified map
	local newMap = game.ServerStorage[mapName]:Clone()
	newMap.Name = "Map"
	newMap.Parent = game.Workspace

	-- Return the new map
	return newMap
end


-- Load the first map
local mapInstance = loadMap("Map1")

-- Get the player
local player = game.Players.LocalPlayer

-- Set the map instance to nil
local mapInstance = nil

-- Constants
local timer = 30 -- Time limit for each map

-- Function to pick a random map from the ServerStorage model
local function pickRandomMap()
	local maps = {"Map1", "Map2", "Map3", "Map4", "Map5"}
	local index = math.random(1, #maps)
	return maps[index]
end

-- Function to show the intermission GUI
local function showIntermission(timeLeft)
	-- Clone the IntermissionGui
	local gui = script.Parent.IntermissionGui:Clone()
	gui.Parent = player:WaitForChild("PlayerGui")

	-- Slide the GUI in from the top
	local tweenInfo = TweenInfo.new(0.5, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)
	local position = UDim2.new(0.5, 0, 0, 0)
	local tween = game:GetService("TweenService"):Create(gui, tweenInfo, {Position = position})
	tween:Play()

	-- Wait for the specified time
	wait(timeLeft)

	-- Slide the GUI out to the top
	tween = game:GetService("TweenService"):Create(gui, tweenInfo, {Position = UDim2.new(0.5, 0, -1, 0)})
	tween:Play()

	-- Destroy the GUI
	gui:Destroy()
end


-- Function to end the game
local function endGame()
	-- Destroy the map
	local map = game.Workspace:FindFirstChild("Map")
	if map then
		map:Destroy()
	end

	-- Teleport the player back to the lobby
	local player = game.Players.LocalPlayer
	player.Character:MoveTo(game.Workspace.Lobby.TeleportLocation.Position)

	-- Show the intermission GUI
	showIntermission(15)
end



-- Function to show the player's placement
local function showPlacement(placement)
	-- Create the GUI
	local gui = script.Parent.PlacementGui:Clone()
	gui.Parent = script.Parent

	-- Set the placement text
	gui.PlacementText.Text = "You placed #" .. placement .. "!"

	-- Slide the GUI in from the right
	local tweenInfo = TweenInfo.new(0.5, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)
	local position = UDim2.new(1, 0, 0.5, 0)
	local tween = game:GetService("TweenService"):Create(gui, tweenInfo, {Position = position})
	tween:Play()

	-- Wait 5 seconds
	wait(5)

	-- Slide the GUI out to the right
	tween = game:GetService("TweenService"):Create(gui, tweenInfo, {Position = UDim2.new(2, 0, 0.5, 0)})
	tween:Play()

	-- Destroy the GUI
	gui:Destroy()
end

-- Function to start the game
local function startGame(player)
	-- Set the timer
	local timeLeft = timer
	game.StarterGui.GameUI.TimerLabel.Text = "Time left: " .. timeLeft .. " seconds"

	-- Set the initial placement and reward
	local placement, reward = 0, 50

	-- Update the timer every second
	while timeLeft > 0 do
		timeLeft = timeLeft - 1
		wait(1)
		game.StarterGui.GameUI.TimerLabel.Text = "Time left: " .. timeLeft .. " seconds"

		-- Check if the player has reached the finish line
		if player.Character:FindFirstChild("FinishLine") or player.Character:FindFirstChild("PlacementPart") then
			-- Increment the placement
			placement = placement + 1

			-- Award the coins
			game.ServerStorage.Coins:AwardCoins(player, reward)

			-- Show the placement
			showPlacement(placement)

			-- Decrement the reward
			reward = reward - 5

			-- End the game if there are no more rewards left
			if reward < 0 then
				break
			end
		end
	end

	-- End the game
	endGame()
end

-- Pick a random map and load it in when a player joins
game.Players.PlayerAdded:Connect(function(player)
	-- Pick a random map and load it in
	local map = pickRandomMap()
	local mapModel = game.ServerStorage:FindFirstChild(map)
	if mapModel then
		local mapInstance = mapModel:Clone()
		mapInstance.Name = "Map"
		mapInstance.Parent = game.Workspace
		if mapInstance and mapInstance:FindFirstChild("TeleportLocation") then
			player.Character:MoveTo(mapInstance.TeleportLocation.Position)
		end
		
	end

	-- Start the game
	startGame(player)
end)

I tried problem solving by using :SetPrimaryPartCFrame() instead of :MoveTo()
but that still didn’t work
and the counter that counts down to the round end is glitching, seeming to have something behind it,

image
I then fixed that by changing some code and removing a duplicate of the label. Turned out to be what the text sets to after this code

	-- Set the timer
	local timeLeft = timer
	game.StarterGui.GameUI.TimerLabel.Text = "Time left: " .. timeLeft .. " seconds"

image
It doesn’t seem to be counting down either.
But after 30 seconds when the round is supposed to end, I get this error: 22:33:19.466 ServerScriptService.GameController:66: attempt to index nil with 'Character' - Server - GameController:66
image
I don’t know why, but if you could help that would be great,
Cheers.

1 Like

Attempt to index nil with “Character” means that character doesn’t exist or wasn’t found

1 Like