Code works fine in Solo, but it messes up a bit in Local Server

So here’s the problem: When I play solo, I only teleport once the event fires, and that’s what I want. However, when they are 2 people in the server, the event seems to fire 2-3 times and it is inconsistent as well. Here is the full script: (I spammed it with breaks, but it sill occurs)

local arenas = game.Workspace:WaitForChild("Arena")
local spawnPoints = game.Workspace:WaitForChild("SpawnPoints")
local teams = game:GetService("Teams")
local spawners = workspace.Spawners

game.ReplicatedStorage.ArenaTPScript.OnServerEvent:Connect(function(player,humanoid)	
	for i, v in pairs(arenas:GetChildren()) do		
		local arenaSpace = Region3.new(v.Position - (v.Size/2),v.Position + (v.Size/2))	
		local parts = game.Workspace:FindPartsInRegion3WithWhiteList(arenaSpace, player.Character:GetDescendants())		
		for _, part in pairs(parts) do			
			if part:FindFirstAncestor(player.Name) then				
				local character = player.Character
				local humanoid = character:WaitForChild("Humanoid")
				
				local arenaTeams = teams[v.Name]:GetPlayers()
				local luckyPlayer = arenaTeams[1]
				print(luckyPlayer)
				local unluckyPlayer = arenaTeams[2]
				
				if #arenaTeams == 2 and v.Name == "1" then
					if unluckyPlayer.Character.Humanoid.Health > 0 and luckyPlayer.Character.Humanoid.Health > 0 then
						unluckyPlayer.Character.HumanoidRootPart.CFrame  = CFrame.new(game.Workspace.SpawnPoints:FindFirstChild("Spawn1_2").Position)
						wait(10)
						if humanoid.Health > 0 and v.Name =="1" then
							luckyPlayer.Character.HumanoidRootPart.CFrame  = CFrame.new(game.Workspace.SpawnPoints:FindFirstChild("Spawn2_0").Position)
							break
						end
					break
					end
				break
				
				elseif v.Name == "1" and humanoid.Health > 0 then
					wait(10)
					if humanoid.Health > 0 and v.Name =="1" then
						player.Character.HumanoidRootPart.CFrame  = CFrame.new(game.Workspace.SpawnPoints:FindFirstChild("Spawn2_0").Position)
						break
					end
				break
					
				elseif humanoid.Health > 0 then
					local playerArena = tonumber(v.Name)
					local nextArena = playerArena - 1
					print(v)	
					for i, spawnArena in pairs (spawnPoints:GetChildren()) do
						local spawnParts = game.Workspace:FindPartsInRegion3WithWhiteList(arenaSpace, player.Character:GetDescendants())	
						for _, part in pairs(spawnParts) do
							wait(0.5)
							if part:FindFirstAncestor(player.Name) then
								local character = player.Character
								local spawnNumber = tonumber(v.Name) - 1
								local spawnFinal = "Spawn2_"..spawnNumber
								player.Character.HumanoidRootPart.CFrame  = CFrame.new(game.Workspace.SpawnPoints:FindFirstChild(spawnFinal).Position)
							break
							end
						break
						end
					break
					end
				break
				end
			break
			end
		break
		end
	end
end)

Sorry for the huge code, but I am desperate because it I can’t continue the game knowing it wont work in multiplayer.

Does the event seen in this code snippet fire multiple times (try printing “1” at the very start of the function)? If so, I need to see the code that fires the event.

It only fires once:

while true do
	for i = 1,10 do
       script.Parent.Text = 10 - i
			if i == 5 then
				spawn(function()
				    for ii = 1,8 do
				        script.Parent.Parent.BorderColor3 = Color3.new(1, 0, 0)
						wait(0.5)
						script.Parent.Parent.BorderColor3 = Color3.new(27, 42, 53)
						wait(0.5)
						ii = ii+1
				    end
				end)
			end
			if i == 10 then
				game.ReplicatedStorage.Respawn:FireAllClients()
			end
			spawn(function()
				if i == 10 then					
					wait(1)
					game.ReplicatedStorage.ArenaTeleport:FireAllClients() --Here is the event--
					script.Parent.Text = "End"
					i = 1
				end
			end)
	        wait(1)		
	end

end

Could you show me the client’s code? The server cannot fire itself.

ahh woops my bad

local PLR = game:GetService("Players")
local player = game.Players.LocalPlayer
local character = player.Character
local humanoid = character:WaitForChild("Humanoid")

game.ReplicatedStorage.ArenaTeleport.OnClientEvent:Connect(function()
game.ReplicatedStorage.ArenaTPScript:FireServer(humanoid)
end)

Ok. So, as I thought, it’s because you’re firing all clients. The reason the event is coming through multiple times is due to you firing more than one person. If you want to fire a single client, index a random player from Players:GetPlayers(). For the inconsistency issue, remember you’re running 3 clients off one computer/network. It may simply be lag.

1 Like

When you use FindFirstAncestor() you aren’t waiting until the object is fully loaded in. Since the isn’t a WaitForAncestor() function, implement a repeat wait until object ~= nil loop to wait for the ancestor to load in, then run the function.

He actually needs to fire all clients for what he is trying to do, although he could have simply used BindableEvents or a ModuleScript to eliminate the need of having to go through the client to communicate between server scripts. You can see in the first script that he is trying to teleport all players, but individually as he uses the player parameter. The part where a lucky and an unlucky player are chosen should not be put in the event function because it is supposed to be ran only once per arena while the rest is per player and per arena. A ModuleScript and a for loop through all players should definitely be used in this case instead of remote events.

From what I see, you seem to have a server script in a player gui because you are using script.Parent.Text and FireAllClients() in the same script, unless it is in a BillboardGui which is unlikely to be the case for a timer script.

Here’s the problem explained with a model:
-Server Fires All Clients
-Client1 fires server back
-Client2 fires server back
-All other clients fire server back

I am assuming you have a .OnServerEvent somewhere that is getting called multiple times, and that is your problem. Instead of firing all players, try firing a random one:

local plrs = game.Players:GetPlayers()
local fire = plrs[math.random[1,#plrs]
RemoteEvent:FireClient(fire)

Note that is would be better practice to call a function instead of having the server fire the client to just fire itself.

function doStuff()
--Code to run
end

--To call:
doStuff()