Hello, I am having a problem with the lobby cloning itself to workpsace. So when the round start, the players character reloads into the map and destroys the lobby during that so the player doesn’t load back into it. I have made a clone in ServerStorage that gets parented into workspace after the round.
One problem is that after 2 or 3 rounds an error will pop up:
I already have the clone function and I don’t know what to do, I have tried to use refrence from old code but it doesn’t work.
Here is the script part where the lobby is cloned back after the round:
currentMap:ClearAllChildren()
LobbyClone.Parent = workspace
for _,Player in pairs(game.Players:GetPlayers()) do
Player:LoadCharacter()
ReplicatedStorage.RemoteEvents.FadeFolder.FadeIn:FireAllClients()
end
end
Here is the script part where the lobby is destroyed:
for _,Player in pairs(game.Players:GetPlayers()) do
workspace:FindFirstChild("Lobby"):Destroy()
Player:LoadCharacter()
end
Here is the whole script:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Serverstorage = game:GetService("ServerStorage")
local TweenService = game:GetService("TweenService")
-- /// Tables
local votes = {}
local voted = {}
-- /// Map Voting Functions
local function highestVote()
local map, highest = nil, 0
for i, v in pairs(votes) do
if v >= highest then
map = i
highest = v
end
end
return map
end
local function voteMaps()
local Status = ReplicatedStorage.RoundSystem:WaitForChild("Status")
votes = {}
voted = {}
local maps = {}
local mapsSelection = ReplicatedStorage.Maps:GetChildren()
repeat
local map = mapsSelection[math.random(1, #mapsSelection)]
if not table.find(maps, map.Name) then
table.insert(maps, map.Name)
votes[map.Name] = 0
end
until #maps == 2
ReplicatedStorage.RemoteEvents.Vote:FireAllClients(maps)
for i = 10, 0, -1 do
Status.Value = i
wait(1)
end
local selectedMap = ReplicatedStorage.Maps[highestVote()]:Clone()
selectedMap.Parent = workspace.CurrentMap
ReplicatedStorage.RemoteEvents.Vote:FireAllClients()
end
ReplicatedStorage.RemoteEvents.Vote.OnServerEvent:Connect(function(player, map)
if votes[map] and not table.find(voted, player.Name) then
table.insert(voted, player.Name)
votes[map] += 1
ReplicatedStorage.RemoteEvents.Update:FireAllClients(votes)
end
end)
local function TimerForm(s)
return string.format("%2i:%02i", s/60, s%60)
end
local function System()
-- /// Variables
local Status = ReplicatedStorage.RoundSystem:WaitForChild("Status")
local ServerStorage = game:GetService("ServerStorage")
local currentMap = workspace.CurrentMap
local Event = ServerStorage:FindFirstChild("Events"):GetChildren()
local LobbyClone = ServerStorage:FindFirstChild("Lobby"):Clone()
local Music = game.Workspace.Music
local IntermissionMusicFolder = Music.Intermission:GetChildren()
local IntermissionMusic = IntermissionMusicFolder[math.random(1, #IntermissionMusicFolder)]
local RoundMusicFolder = Music.RoundMusic:GetChildren()
local RoundMusic = RoundMusicFolder[math.random(1, #RoundMusicFolder)]
local VictoryMusic = Music.Victory
local RoundValue = false
-- /// Game Loop
while task.wait() do
print("Intermission")
IntermissionMusic:Play()
for i = 30, 0, -1 do
Status.Value = i
wait(1)
end
Status.Value = "Vote for a Map!"
print("Map Voting")
-- /// Map Voting
voteMaps()
ReplicatedStorage.RemoteEvents.FadeFolder.FadeOut:FireAllClients()
task.wait(0.3)
for _,Player in pairs(game.Players:GetPlayers()) do
workspace:FindFirstChild("Lobby"):Destroy()
Player:LoadCharacter()
end
local selectedMap = ReplicatedStorage.Maps[highestVote()]:Clone()
ReplicatedStorage.RemoteEvents.FadeFolder.FadeIn:FireAllClients()
for i = 5, 0, -1 do
Status.Value = i
wait(1)
end
IntermissionMusic:Stop()
-- /// Event Selection
local selectedEvent = Event[math.random(1, #Event)]:Clone()
Status.Value = selectedEvent.Name
-- /// Spawning Event
local mapModel = selectedMap
local eventSpawn = nil
for _,v in pairs (mapModel:GetDescendants()) do
if (v.Name == "EventSpawn") then
print("Found Event Spawn Point")
eventSpawn = v
break
end
end
if (eventSpawn == nil) then
error("EventSpawn not found in map model")
end
print("Game Started")
-- /// Teleporting Event
selectedEvent.Parent = workspace.Events
selectedEvent:PivotTo(eventSpawn.CFrame)
local CurrentEvent = game.Workspace.Events
-- /// Round
RoundValue = true
for i = 120, 0, -1 do
Status.Value = TimerForm(i)
wait(1)
end
Status.Value = "Game Over"
CurrentEvent:ClearAllChildren()
RoundValue = false
print("Game Ended")
VictoryMusic:Play()
task.wait(5)
ReplicatedStorage.RemoteEvents.FadeFolder.FadeOut:FireAllClients()
task.wait(0.3)
currentMap:ClearAllChildren()
LobbyClone.Parent = workspace
for _,Player in pairs(game.Players:GetPlayers()) do
Player:LoadCharacter()
ReplicatedStorage.RemoteEvents.FadeFolder.FadeIn:FireAllClients()
end
end
-- /// Restart
end
spawn(System)
How do I fix this problem?