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)