Players receiving message at different times breaking game

Hi everyone. My game is breaking due to players recieving a message at different times. I have figured out that it is dependant on when the player joins. Any solutions? (The serverscript is mostly irrelevant)

Serverscript:

local playersPlaying = {}
local parkourroundtime = 40
local finish = game.Workspace.FinishPart
local someonehaswon = false

game:GetService("ReplicatedStorage"):WaitForChild("ParkourRaceJoinFire").OnServerEvent:Connect(function(player)
	table.insert(playersPlaying, player)
	print("There are/is currently "..#playersPlaying.." player(s) playing!")
	game:GetService("ReplicatedStorage"):WaitForChild("ParkourRaceStartFire").OnServerEvent:Connect(function()
		someonehaswon = false
		
		for i,v in pairs(playersPlaying) do
			v.Character:WaitForChild("HumanoidRootPart").CFrame = game.Workspace.ParkourRaceStartPart.CFrame
			if v.Backpack:FindFirstChild("GravityCoil") then
				v.Backpack.GravityCoil:Destroy()
			end
		end
		
		finish.Touched:Connect(function(hit)
			if someonehaswon == false then
				if hit.Parent:FindFirstChild("Humanoid") then
					print(hit.Parent.Name.." has won! - SERVER")
					
					hit.Parent:WaitForChild("leaderstats").Coins.Value += 50
					
					game:GetService("ReplicatedStorage"):WaitForChild("ParkourWinFire"):FireAllClients(hit.Parent.Name)
					someonehaswon = true
					
					for i,v in pairs(playersPlaying) do
						v.Character:WaitForChild("Humanoid").Health = 0
					end
					task.wait(0.1)
					for k in pairs(playersPlaying) do
						playersPlaying[k] = nil
					end
				end
			end
		end)	
		
		wait(parkourroundtime)
		
		if someonehaswon == false then
			print("NOBODY WON! - SERVER")
			game:GetService("ReplicatedStorage"):WaitForChild("ParkourNobodyWon"):FireAllClients()
			for i,v in pairs(playersPlaying) do
				v.Character:WaitForChild("Humanoid").Health = 0
			end
			
			for k in pairs(playersPlaying) do
				playersPlaying[k] = nil
			end
		end
	
		print("Should be nil:")
		print(playersPlaying)
		print("Round Ended!")
	end)
end)

Localscript:

local winnerframe = script.Parent.Parent.WinnerFrame
local winnername = winnerframe.Winnername
local coinsamt = winnerframe.CoinsText

winnerframe.Visible = false

print(#game.Players:GetPlayers())
local ingame = script.Parent.InGame
ingame.Value = false
local frame = script.Parent
frame.Visible = false

local function promptjoin()
	print("PROMPT TO JOIN SENT")
	frame.Visible = true
	frame.Join.MouseButton1Click:Connect(function()
		frame.Visible = false
		game:GetService("ReplicatedStorage"):WaitForChild("ParkourRaceJoinFire"):FireServer(game.Players.LocalPlayer)
	end)
	frame.Nah.MouseButton1Click:Connect(function()
		frame.Visible = false
	end)
	wait(5)
	frame.Visible = false
	game:GetService("ReplicatedStorage"):WaitForChild("ParkourRaceStartFire"):FireServer()
end

game:GetService("ReplicatedStorage"):WaitForChild("ParkourWinFire").OnClientEvent:Connect(function(playerwhowon)
	print(playerwhowon.." WON! - CLIENT")
	winnerframe.Visible = true
	winnername.Text = playerwhowon
	coinsamt.Text = "+50 Coins!"
	task.wait(5)
	winnerframe.Visible = false
end)

game:GetService("ReplicatedStorage"):WaitForChild("ParkourNobodyWon").OnClientEvent:Connect(function()
	print("NOBODY WON! - CLIENT")
	winnerframe.Visible = true
	winnername.Text = "Nobody Won!"
	coinsamt.Text = "+0 Coins!"
	task.wait(5)
	winnerframe.Visible = false
end)


while true do
	task.wait(300)
	if #game.Players:GetPlayers() > 1 and ingame.Value == false then
		promptjoin()
		print("enough players in game :)! ("..#game.Players:GetPlayers()..")")
	else
		print("NOT ENOUGH PLAYERS OR GAME ALREADY RUNNING!")
	end
end

This is kinda a disaster since I didn’t know of this error until I published and tested with friends. The winnermessage just appears at random times for different players. Any help at all would be so much appreciated.

I only see one thing wrong with this code and it has nothing to do with the timings between players recieving the event. Is it possible you/they were just lagging? Perhaps try running the game on a local server in studio and check if theres still delay.

Heres the fix for the thing I saw wrong (Unlikely will fix code but since I noticed thought I should atleast fix it)

local playersPlaying = {}
local parkourroundtime = 40
local finish = game.Workspace.FinishPart
local someonehaswon = false

game:GetService("ReplicatedStorage"):WaitForChild("ParkourRaceJoinFire").OnServerEvent:Connect(function(player)
	table.insert(playersPlaying, player)
	print("There are/is currently "..#playersPlaying.." player(s) playing!")
	game:GetService("ReplicatedStorage"):WaitForChild("ParkourRaceStartFire").OnServerEvent:Connect(function()
		someonehaswon = false
		
		for i,v in pairs(playersPlaying) do
			v.Character:WaitForChild("HumanoidRootPart").CFrame = game.Workspace.ParkourRaceStartPart.CFrame
			if v.Backpack:FindFirstChild("GravityCoil") then
				v.Backpack.GravityCoil:Destroy()
			end
		end
		
		local connection = finish.Touched:Connect(function(hit)
			if someonehaswon == false then
				if hit.Parent:FindFirstChild("Humanoid") then
					print(hit.Parent.Name.." has won! - SERVER")
					
					hit.Parent:WaitForChild("leaderstats").Coins.Value += 50
					
					game:GetService("ReplicatedStorage"):WaitForChild("ParkourWinFire"):FireAllClients(hit.Parent.Name)
					someonehaswon = true
					
					for i,v in pairs(playersPlaying) do
						v.Character:WaitForChild("Humanoid").Health = 0
					end
					task.wait(0.1)
					for k in pairs(playersPlaying) do
						playersPlaying[k] = nil
					end
				end
			end
		end)	
		
		wait(parkourroundtime)
		
		if someonehaswon == false then
			print("NOBODY WON! - SERVER")
			game:GetService("ReplicatedStorage"):WaitForChild("ParkourNobodyWon"):FireAllClients()
			for i,v in pairs(playersPlaying) do
				v.Character:WaitForChild("Humanoid").Health = 0
			end
			
			for k in pairs(playersPlaying) do
				playersPlaying[k] = nil
			end
		end
		connection:Disconnect()
		print("Should be nil:")
		print(playersPlaying)
		print("Round Ended!")
	end)
end)
1 Like

Thanks, but what exactly did this fix? My problem was that players were recieving this message at different times and it just wasn’t working. I believe players recieved the message in the order that they joined leading me to think that it has something to do with the loop in the localscript starting at different times. Do you know how I could fix this?

This fixed a possible memory leak issue. Every time a round started you were creating a new touched event without disconnecting it. Let’s say the server was up for multiple hours it could make a real impact.

Also I looked at the code again and I think its because you are firing StartRace for every player. Switch it to a system where when enough players fire RaceJoin you start the race on the server. That way only 1 event will be fired to all the clients.

I don’t have access to studio to test for errors but here’s an example:

local playersPlaying = {}
local parkourroundtime = 40
local finish = game.Workspace.FinishPart
local someonehaswon = false
local running = false

game:GetService("ReplicatedStorage"):WaitForChild("ParkourRaceJoinFire").OnServerEvent:Connect(function(player)
	table.insert(playersPlaying, player)
	print("There are/is currently "..#playersPlaying.." player(s) playing!")
	-- Just an example you could start the game any way you like.
	if #playersPlaying > 1 and not running then
		running = true
		StartGame()
		running = false
	end
end)
function StartGame()
		someonehaswon = false
		
		for i,v in pairs(playersPlaying) do
			v.Character:WaitForChild("HumanoidRootPart").CFrame = game.Workspace.ParkourRaceStartPart.CFrame
			if v.Backpack:FindFirstChild("GravityCoil") then
				v.Backpack.GravityCoil:Destroy()
			end
		end
		
		local connection = finish.Touched:Connect(function(hit)
			if someonehaswon == false then
				if hit.Parent:FindFirstChild("Humanoid") then
					print(hit.Parent.Name.." has won! - SERVER")
					
					hit.Parent:WaitForChild("leaderstats").Coins.Value += 50
					
					game:GetService("ReplicatedStorage"):WaitForChild("ParkourWinFire"):FireAllClients(hit.Parent.Name)
					someonehaswon = true
					
					for i,v in pairs(playersPlaying) do
						v.Character:WaitForChild("Humanoid").Health = 0
					end
					task.wait(0.1)
					for k in pairs(playersPlaying) do
						playersPlaying[k] = nil
					end
				end
			end
		end)	
		
		wait(parkourroundtime)
		
		if someonehaswon == false then
			print("NOBODY WON! - SERVER")
			game:GetService("ReplicatedStorage"):WaitForChild("ParkourNobodyWon"):FireAllClients()
		for i,v in pairs(playersPlaying) do
			v.Character:WaitForChild("Humanoid").Health = 0
		end
		
		for k in pairs(playersPlaying) do
			playersPlaying[k] = nil
		end
	end
	connection:Disconnect()
	print("Should be nil:")
	print(playersPlaying)
	print("Round Ended!")
	
end

1 Like