Deleting a Map from the workspace

Hello, I have a script that deletes a map from the workspace when the round is ended. It works perfectly fine when I’m the only person in the server, but when there are multiple people it breaks. How do I fix this?

This depends on the code when a higher player count than 1 are involved. Can you provide the code necessary around the issue?

local a = 1
local Players = game:GetService(“Players”)

game.ReplicatedStorage.RoundEvent.OnServerEvent:Connect(function(plr)
print(“Round Started”)
if a == 1 then
wait()
local Clone1 = game.ReplicatedStorage.TestMap1:Clone()
Clone1.Parent = workspace
for _, plr in pairs(game.Players:GetChildren()) do
wait(1)
plr.Character.HumanoidRootPart.Position = game.Workspace.TestMap1.Spawn.Position
end
a = 2
game.Workspace.TestMap1.Winner.Touched:Connect(function(hit)
local Winner = Players:GetPlayerFromCharacter(hit.Parent)
Winner.leaderstats.Wins.Value = Winner.leaderstats.Wins.Value + 1
game.Workspace.TestMap1.Winner:Destroy()
game.ReplicatedStorage.WinnerEvent:FireClient(plr, hit)
wait(3)
game.Workspace.TestMap1:Destroy()
game.ReplicatedStorage.TimerValue.Value = 30
end)
elseif a == 2 then
wait()
local Clone2 = game.ReplicatedStorage.TestMap2:Clone()
Clone2.Parent = workspace
for _, plr in pairs(game.Players:GetChildren()) do
wait(1)
plr.Character.HumanoidRootPart.Position = game.Workspace.TestMap2.Spawn.Position
end
a = 1
game.Workspace.TestMap2.Winner.Touched:Connect(function(hit)
local Winner = Players:GetPlayerFromCharacter(hit.Parent)
Winner.leaderstats.Wins.Value = Winner.leaderstats.Wins.Value + 1
game.Workspace.TestMap2.Winner:Destroy()
game.ReplicatedStorage.WinnerEvent:FireClient(plr, hit)
wait(3)
game.Workspace.TestMap1:Destroy()
game.ReplicatedStorage.TimerValue.Value = 30
end)
end
end)

(I dont know how to make a code block)

There are no Errors in the output, it just doesn’t work

local a = 1
local Players = game:GetService("Players")

game.ReplicatedStorage.RoundEvent.OnServerEvent:Connect(function(plr)
	print("Round Started")
	if a == 1 then
		wait()
		local Clone1 = game.ReplicatedStorage.TestMap1:Clone()
		Clone1.Parent = workspace
		for _, plr in pairs(game.Players:GetChildren()) do
			wait(1)
			plr.Character.HumanoidRootPart.Position = game.Workspace.TestMap1.Spawn.Position
		end
		a = 2
		game.Workspace.TestMap1.Winner.Touched:Connect(function(hit) -- oh no
			local Winner = Players:GetPlayerFromCharacter(hit.Parent)
			Winner.leaderstats.Wins.Value = Winner.leaderstats.Wins.Value + 1
			game.Workspace.TestMap1.Winner:Destroy()
			game.ReplicatedStorage.WinnerEvent:FireClient(plr, hit)
			wait(3)
			game.Workspace.TestMap1:Destroy()
			game.ReplicatedStorage.TimerValue.Value = 30
		end)
	elseif a == 2 then
		wait()
		local Clone2 = game.ReplicatedStorage.TestMap2:Clone()
		Clone2.Parent = workspace
		for _, plr in pairs(game.Players:GetChildren()) do
			wait(1)
			plr.Character.HumanoidRootPart.Position = game.Workspace.TestMap2.Spawn.Position
		end
		a = 1
		game.Workspace.TestMap2.Winner.Touched:Connect(function(hit)
			local Winner = Players:GetPlayerFromCharacter(hit.Parent)
			Winner.leaderstats.Wins.Value = Winner.leaderstats.Wins.Value + 1
			game.Workspace.TestMap2.Winner:Destroy()
			game.ReplicatedStorage.WinnerEvent:FireClient(plr, hit)
			wait(3)
			game.Workspace.TestMap1:Destroy()
			game.ReplicatedStorage.TimerValue.Value = 30
		end)
	end
end)

Here’s the code in proper indentation. There’s a lot of things to note:

  1. This code is exploitable, when it comes to OnServerEvent to perform something big. Why would you allow a client to start something immediately? This will certainly cause the game to break because multiple clients are able to fire the event.
  2. You are connecting functions to Touched endlessly, which causes a significant performance impact over time. Do not connect functions in a function/loop/scope, unless you know what you are doing. Oh, and apparently the connection is deleted after the map is done.
  3. It seems game.ReplicatedStorage.TimerValue.Value = 30 is never executed, because the code wasn’t fast enough? I wouldn’t place it in this order and would have swapped them around.
  4. The code can be easily simplified to something that “fits all scenarios”.
  5. Questionable method of teleporting players was used. SetPrimaryPartCFrame function of their character could work, but it isn’t a big issue.
  6. A lot of simplification could be done as well, without changing the code’s performance.
2 Likes