Okay below is the function im using. The problem im having is its cloning the map multiple times if there is more then one player server. I'm guessing that's because its in the i,v in pairs loop. However, I cant take it out of that loop or it breaks the script. So is there a way I can check to see if the map exists or not then if it doesn't clone it but if it does then just keep moving on with the script?
function intermission() for z = 45,1, -1 do wait(1) for i,v in pairs(game.Players:GetPlayers()) do GUI1 = v.PlayerGui.Intermission.Frame GUI2 = v.PlayerGui.NewsBar.Frame MapName = v.PlayerGui.StartRound.Frame.Map GUI3 = GUI1.Title Time = GUI1.Seconds GUI1.Visible = true GUI2.Visible = true Time.Text = z if z == 1 then num = 2 maps = {"Abandoned Church", "Campsite", "Facility"} m = math.random(1, num) t = game.Lighting:FindFirstChild(maps[m]):clone() t.Parent = game.Workspace t:MakeJoints() GUI3.Text = "Loading Map..." Time.Text = " " MapName.Text = t.Name wait(7) TeleportToMap() end end end end
Normally, when you have objects, you check if they have something by just use []
or .
:
if string.sub then -- There is a `sub` in the `string` library! end if string["sub"] ~= nil then -- This is the same end if not string.sub == nil then -- This is the same, too end
However, for ROBLOX objects, if the object isn't there, you will get an error, not nil
.
On a ROBLOX object, instead use :FindFirstChild
:
if workspace:FindFirstChild("BlueTaslem") then -- It didn't return `nil`, so there is a BlueTaslem in the workspace end
Nonetheless, Instead of applying this, just fix your script! Don't cover up problems, solve them! That code shouldn't be happening over and over -- that is the problem.
First: checking if z == 1
isn't the wise way to do it. Instead, just put that code after the loop -- what you mean by if z == 1
is that the loop is done, so use that fact!
The only issue is MapName
-- which you'll just add another short loop to do. However, this is not the best way to structure this, and, for instance, won't work with FilteringEnabled.
function intermission() for time = 45,1, -1 do wait(1) for _, player in pairs(game.Players:GetPlayers()) do local GUI1 = player.PlayerGui.Intermission.Frame local GUI2 = player.PlayerGui.NewsBar.Frame local Time = GUI1.Seconds GUI1.Visible = true GUI2.Visible = true Time.Text = time end num = 2 maps = {"Abandoned Church", "Campsite", "Facility"} m = math.random(1, num) t = game.Lighting:FindFirstChild(maps[m]):clone() t.Parent = game.Workspace t:MakeJoints() for _, player in pairs(game.Players:GetPlayers()) do local MapName = player.PlayerGui.StartRound.Frame.Map local GUI3 = GUI1.Title GUI3.Text = "Loading Map..." Time.Text = " " MapName.Text = t.Name end wait(7) TeleportToMap() end end
A more correct way to manage GUIs would be something like this:
-- A LOCALSCRIPT in the PlayerGui (i.e., put it in the StarterGui) local player = game.Players.LocalPlayer local gui = player:WaitForChild("PlayerGui") local intermissionFrame = gui.Intermission.Frame local newsBar = gui.NewsBar.Frame local globalMessage = game.ReplicatedStorage.Status local globalInt = game.ReplicatedStorage.InIntermission function update() local msg = globalMessage.Value if globalInt.Value then -- In intermission newsBar.Visible = true intermissionFrame.Visible = true; intermissionFrame.Seconds.Text = msg else intermissionFrame.Visible = false newsBar.Visible = false intermissionFrame.Visible = false gui.StartRound.Frame.Map.Text = msg end end globalMessage.Changed:connect(update) globalInt.Changed:connect(update) update()
Our game round manager is now much simpler:
function intermission() for time = 45,1, -1 do wait(1) game.ReplicatedStorage.InIntermission.Value = true game.ReplicatedStorage.Status.Value = time end local maps = {"Abandoned Church", "Campsite", "Facility"} local m = math.random(1, #maps) local t = game.Lighting:FindFirstChild(maps[m]):Clone() t.Parent = game.Workspace t:MakeJoints() game.ReplicatedStorage.InIntermission.Value = false game.ReplicatedStorage.Status.Value = "Loading Map..." wait(7) TeleportToMap() end
where InIntermission
is a BoolValue in ReplicatedStorage and Status
is a StringValue in ReplicatedStorage.
Easy, just check if it's nil using an If Statement.
For example:
if game.Workspace.Player1 then --If you just leave it like that then it just sees if the Player2 is true. print("Player1 is here!") end if game.Workspace.Player2 == nil then --Oh, this is different. you can also use "== nil" to see if it exists. print("Player2 is here also!") end if game.Workspace.Player3 ~= nil then --Using "==" and "~=" do basicly the same thing. print("Player3 has joined the game!") end
Now to add it to the script.
function intermission() for z = 45,1, -1 do wait(1) for i,v in pairs(game.Players:GetPlayers()) do GUI1 = v.PlayerGui.Intermission.Frame GUI2 = v.PlayerGui.NewsBar.Frame MapName = v.PlayerGui.StartRound.Frame.Map GUI3 = GUI1.Title Time = GUI1.Seconds GUI1.Visible = true GUI2.Visible = true Time.Text = z if z == 1 then num = 2 maps = {"Abandoned Church", "Campsite", "Facility"} m = math.random(1, num) t = game.Lighting:FindFirstChild(maps[m]):clone() t.Parent = game.Workspace t:MakeJoints() GUI3.Text = "Loading Map..." Time.Text = " " MapName.Text = t.Name wait(7) TeleportToMap() elseif workspace["Abandoned Church"] or workspace.Campsite or workspace.Facility then return end end end end end