Help with correct ordering

So I have an egg hatching system and that the ordering is wrong when sending the server chat message.

So if I hatch: (“Dog”, “Cat”, “Dog”)
then it would print something like: (“Cat”, “Dog”, "Dog)

How can I fix this?

Code:

function module.SendServerMessage(player, petData)
	local rarityData = module.CheckRarity(petData.RarityName)

	if rarityData then
		if rarityData.SentTo == "Server" then
			local data = {PlayerName = player.Name, PetData = petData}
			Remotes.SendHatchMessage:FireAllClients("Server", data)

		elseif rarityData.SentTo == "Global" then
			module.SendGlobalMessage(player, petData)
		end
	end
end

-----------------------------------------------------------------------

function module.GetRandomPet(eggData)
	local TotalWeight = 0

	for _, pet in ipairs(eggData.Pets) do
		TotalWeight += pet.Chance
	end

	local Chance = math.random(0, TotalWeight)
	local Counter = 0

	for _, pet in ipairs(eggData.Pets) do
		Counter += pet.Chance

		if Chance <= Counter then
			return pet
		end
	end
end

function module.HatchEggs(player, eggData, amount, eggName)
	local petsTable = {}
	
	for i = 1, amount, 1 do
		local pet = module.GetRandomPet(eggData)
		table.insert(petsTable, pet)
		
		print(pet.PetName)
		
		Remotes.GivePet:Fire(player, pet)
		module.SendServerMessage(player, pet)
	end
	
	Remotes.HatchEgg:FireClient(player, petsTable, eggName, amount)
end

What’s the chance the client simply receives the events from the server in an incorrect order? You could try resolving it by sending all of the pets at once within the :FireAllClients()/:FireClient()

Based on the way you do it, I think you could make the code collect the pets’ names in the function module.HatchEggs while also accounting for the rarity and sending the data to the client after you list all the names.

I don’t think the client is incorrect.

Remotes.HatchEgg.OnClientEvent:Connect(function(pets, eggName, amount)
	if amount == 1 then
		HatchEggModule.Hatch1(eggName, table.unpack(pets))
		
	elseif amount == 3 then
		HatchEggModule.Hatch3(eggName, pets)
	end
end)

part of hatching module:

for i = 1, 3, 1 do
			local petData = petsTable[i]
			local pet = module.GetPet(petData.PetName)
			
			--[[
			print(petData.PetName)
			print("------------------------")
			]]
			
			local eggOffset = eggOffsets[i]
			local petOffset = petOffsets[i]

			HatchModule.Hatch(egg, pet, eggOffset, petOffset, petData)
		end

That is not what I meant, you are sending one pet per request. The client could be receiving the packets in a different order.
image

1 Like

Maybe, let me try this.

Cause it prints differently:

function module.SendServerMessage(player, petData)
	local rarityData = module.CheckRarity(petData.RarityName)

	if rarityData then
		if rarityData.SentTo == "Server" then
			local data = {PlayerName = player.Name, PetData = petData}
			Remotes.SendHatchMessage:FireAllClients("Server", data)
			
			print("test2: "..petData.PetName)

		elseif rarityData.SentTo == "Global" then
			module.SendGlobalMessage(player, petData)
		end
	end
end

function module.HatchEggs(player, eggData, amount, eggName)
	local petsTable = {}
	
	for i = 1, amount, 1 do
		local pet = module.GetRandomPet(eggData)
		table.insert(petsTable, pet)
		
		print("test1: "..pet.PetName)
		print("-----------------")
		
		Remotes.GivePet:Fire(player, pet)
		module.SendServerMessage(player, pet)
	end
	
	Remotes.HatchEgg:FireClient(player, petsTable, eggName, amount)

	local IsHatching = player:WaitForChild("IsHatching")
	IsHatching.Value = true
end

That can’t be the issue, the post above shows the prints sending different pet orders.

You put the print("-----------------") before running module.SendServerMessage, of course it presents an incorrect result. If the print were to be placed in the right spot (after module.SendServerMessage is called), the names would match just fine.

1 Like

I don’t see anything wrong with the server message system:

local function SendToServer(plrName, petData)
	local message = GetMessage(plrName, petData)
	Channel:DisplaySystemMessage(message)
end

local function SendToGlobal(plrName, petData)
	local message = GetMessage(plrName, petData)
	Channel:DisplaySystemMessage(message)
end

Remotes.SendHatchMessage.OnClientEvent:Connect(function(sentTo, data)
	if sentTo == "Server" then
		SendToServer(data.PlayerName, data.PetData)
		
	elseif sentTo == "Global" then
		SendToGlobal(data.PlayerName, data.PetData)
	end
end)

oh wait…

it seems to print differently:

Remotes.SendHatchMessage.OnClientEvent:Connect(function(sentTo, data)
	if sentTo == "Server" then
		print("client: "..data.PetData.PetName)
		print("--------------------------")
		SendToServer(data.PlayerName, data.PetData)
		
	elseif sentTo == "Global" then
		SendToGlobal(data.PlayerName, data.PetData)
	end
end)

my bad again, it seems to print the same.

Oh wait. I see the problem:

local function SendToServer(plrName, petData)
	local message = GetMessage(plrName, petData)
	Channel:DisplaySystemMessage(message)
	
	print("client: "..petData.PetName)
end

local function SendToGlobal(plrName, petData)
	local message = GetMessage(plrName, petData)
	Channel:DisplaySystemMessage(message)
end

Remotes.SendHatchMessage.OnClientEvent:Connect(function(sentTo, data)
	if sentTo == "Server" then
		SendToServer(data.PlayerName, data.PetData)
		
	elseif sentTo == "Global" then
		SendToGlobal(data.PlayerName, data.PetData)
	end
end)

Im so confused:

This is way off from what I initially assumed? It seems that the new chat system is somehow messing up the message order? I cannot explain it otherwise.

I suggest just doing the thing I initially suggested: putting all of the pet data objects into two tables, based on RarityData.SentTo and then replicating all at once in two :FireAllClients()/:FireClient() if pets are found in said tables.

so something like:

module.SendServerMessage(player, petsTable)

Have two tables, one for “Server” rarity and one for “Global” SentTo. Iterate through the chosen pets and insert them into said tables if rarityData exists and rarityData.SentTo is either “Server” or “Global” (decide which table to insert to based on that, essentially)

Then, verify that the table isn’t empty with next(table) ~= nil (one of the methods) after you finish the for loop in module.HatchEggs, and if it’s not empty - send it to the player (or all players) via :FireClient() and :FireAllClients()

Example of what I mean:

local serverDataList = {}
local globalDataList = {}
local petList = {}

for i = 1, amount do
	local pet = module.GetRandomPet(eggData)
	table.insert(petList, pet)

	Remotes.GivePet:Fire(player, pet)
	
	local rarityData = module.CheckRarity(pet.RarityName)
	if not rarityData then
		continue
	end

	local sentTo = rarityData.SentTo
	if sentTo == "Server" then
		table.insert(serverDataList, pet)
	elseif sentTo == "Global" then
		table.insert(globalDataList, pet)
	end
end

Remotes.HatchEgg:FireClient(player, petList, eggName, amount)
if next(serverDataList) then
	Remotes.SendHatchMessage:FireAllClients("Server", {
		PlayerName = player.Name,
		PetList = serverDataList
	})
end

if next(globalDataList) then
	-- // Modify the SendGlobalMessage function to support such input
	-- // This is a table filled with pet objects.
	module.SendGlobalMessage(player, globalDataList)
end

I tried this but it isn’t in order still.

Looking at the first video you sent, where your output printed the correct order, and your chat not, I think this issue is in your chat function. I understand this can’t help you that much but maybe it can fix your issue, currently dont have the time to read your whole code :smiley:

1 Like