Issue with my code

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I’m trying to create a system which takes a finished game’s statistics and calculates the reward due to each player based on particular categories (players hit, players killed, highest killstreak).

  1. What is the issue? Include screenshots / videos if possible!
    So i’m calculating for XP and coins acquired by each player. The output shows that the treatment functions calculate those amounts correctly in respect to the worth tables that are being passed.

Although the final print always shows that coins[“Players”] always has the same values as XP[“Players”], this is not the same for coins[“TopPlayers”] and XP["TopPlayers].

For some reason XP[“Players”] and coins[“Players”] share the same values although going through different treatments. Something must be going wrong in the CalcGameResults function.

I’m certain theres a simple mistake somewhere but i’m having a lot of troubles at finding it.
3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

local coins_worth = {
	["PlayersKilled"] = 2,
	["PartsDestroyed"] = 1/30,
	["SpawnpointsDestroyed"] = 5,
	["TopPlayers"] = {
		["PlayersKilled"] = 15,
		["PartsDestroyed"] = 15,
		["SpawnpointsDestroyed"] = 15
	}
}

local xp_worth = {
	["PlayersKilled"] = 10,
	["PartsDestroyed"] = 5/30,
	["SpawnpointsDestroyed"] = 25,
	["TopPlayers"] = {
		["PlayersKilled"] = 100,
		["PartsDestroyed"] = 100,
		["SpawnpointsDestroyed"] = 100
	}
}


local default_player_stats = {
	["PlayersKilled"] = 0,
	["PartsDestroyed"] = 0,
	["SpawnpointsDestroyed"] = 0,
}

local treatment = {
	["Players"] = function(GameStats, worth)
		local retVal = {}
		
		for user_id, player_stats in GameStats do
			retVal[user_id] = default_player_stats
			
			for stat, value in player_stats do
				print("Calc results", stat, value, worth[stat], value * worth[stat])
				retVal[user_id][stat] = value * worth[stat]
			end
		end
		
		print("treatment ret val - ", retVal)
		return retVal
	end,
	
	["TopPlayers"] = function(GameStats, worth)
		local retVal = {}
		
		for stat, user_id in GameStats do
			retVal[stat] = {["user_id"] = user_id, ["worth"] = worth["TopPlayers"][stat] }
		end
		
		return retVal
	end,
}
function GameReward:CalcGameResults(GameStats)
	local retVal = {["coins"] = {}, ["XP"] = {}}
	
	print("game vals - ", GameStats)
	for i, v in GameStats do 
		retVal["coins"][i] = treatment[i](v, coins_worth)
		retVal["XP"][i] = treatment[i](v, xp_worth)
	end
	
	print("ret val - ", retVal)
	return retVal
end

The output is as follows:

  23:07:13.845  Calc results SpawnpointsDestroyed 0 5 0  -  Server - GameReward:44
  23:07:13.845  Calc results PlayersKilled 0 2 0  -  Server - GameReward:44
  23:07:13.846  Calc results PartsDestroyed 3109 0.03333333333333333 103.63333333333333  -  Server - GameReward:44
  23:07:13.846  treatment ret val -   ▶ {...}  -  Server - GameReward:49
  23:07:13.846  Calc results SpawnpointsDestroyed 0 25 0  -  Server - GameReward:44
  23:07:13.846  Calc results PlayersKilled 0 10 0  -  Server - GameReward:44
  23:07:13.846  Calc results PartsDestroyed 3109 0.16666666666666666 518.1666666666666  -  Server - GameReward:44
  23:07:13.847  treatment ret val -   ▶ {...}  -  Server - GameReward:49
  23:07:13.847  ret val -   ▼  {
                    ["XP"] =  ▼  {
                       ["Players"] =  ▼  {
                          [1045238997] =  ▼  {
                             ["PartsDestroyed"] = 518.1666666666666,
                             ["PlayersKilled"] = 0,
                             ["SpawnpointsDestroyed"] = 0
                          }
                       },
                       ["TopPlayers"] =  ▼  {
                          ["PartsDestroyed"] =  ▼  {
                             ["user_id"] = 1045238997,
                             ["worth"] = 100
                          }
                       }
                    },
                    ["coins"] =  ▼  {
                       ["Players"] =  ▼  {
                          [1045238997] =  ▼  {
                             ["PartsDestroyed"] = 518.1666666666666,
                             ["PlayersKilled"] = 0,
                             ["SpawnpointsDestroyed"] = 0
                          }
                       },
                       ["TopPlayers"] =  ▼  {
                          ["PartsDestroyed"] =  ▼  {
                             ["user_id"] = 1045238997,
                             ["worth"] = 15
                          }
                       }
                    }
                 }  -  Server - GameReward:102

The logs show that for each player, the values for PartsDestroyed, PlayersKilled, and SpawnpointsDestroyed are being correctly calculated and stored in both XP and coins for both “Players” and “TopPlayers” categories.

The issue might be related to how you are observing or interpreting the values in the output. It’s normal for XP["Players"] and coins["Players"] to have the same values because the calculations are based on the same player statistics. The difference between XP and coins lies in the multiplier values defined in the coins_worth and xp_worth tables.

Modified script

function GameReward:CalcGameResults(GameStats)
    local retVal = {["coins"] = {}, ["XP"] = {}}
    
    print("game vals - ", GameStats)
    for user_id, stats in pairs(GameStats) do
        retVal["XP"][user_id] = default_player_stats
        retVal["coins"][user_id] = default_player_stats

        for stat, value in pairs(stats) do
            retVal["XP"][user_id][stat] = value * xp_worth[stat]
            
            if type(coins_worth[stat]) == "table" then
                retVal["coins"][user_id][stat] = coins_worth[stat][user_id] and coins_worth[stat][user_id]["worth"] or 0
            else
                retVal["coins"][user_id][stat] = value * coins_worth[stat]
            end
        end
    end
    
    print("ret val - ", retVal)
    return retVal
end
1 Like

the GameStats variable is structured as follows,

game vals -   ▼  {
                    ["Players"] =  ▼  {
                       [1045238997] =  ▼  {
                          ["PartsDestroyed"] = 1842,
                          ["PlayersKilled"] = 0,
                          ["SpawnpointsDestroyed"] = 0
                       }
                    },
                    ["TopPlayers"] =  ▼  { 
                       ["PartsDestroyed"] = 1045238997
                    }
                 }  -  Server - GameReward:96

I would like to keep the treatments approach but i still don’t understand why the values for coins[“Players”], and XP[“Players”] would continue to be the same, i’m definitely missing something.

Your treatment functions seem to be calculating the values correctly, but it’s possible that there’s an issue in the loop where you’re applying these treatments to individual players.

for i, v in GameStats do 
    retVal["coins"][i] = treatment[i](v, coins_worth)
    retVal["XP"][i] = treatment[i](v, xp_worth)
end

Here, i appears to represent the treatment type (“Players” or “TopPlayers”), and v is the associated data. If there’s an issue with the values not updating as expected, it might be worth checking the contents of v and ensuring that the data structure matches your expectations.

Also, make sure that the treatments themselves are correctly updating the values for each player. For example, check the treatment["Players"] function to ensure that it’s correctly calculating and assigning values for each player.

If the issue persists, consider adding additional print statements or using the Roblox Studio debugger to inspect the values of GameStats, coins_worth, xp_worth, and the intermediate results within the treatment functions.

Use this

for treatmentType, playerStats in pairs(GameStats) do 
    retVal["coins"][treatmentType] = treatment[treatmentType](playerStats, coins_worth)
    retVal["XP"][treatmentType] = treatment[treatmentType](playerStats, xp_worth)
end

In this loop, treatmentType will take values like “Players” or “TopPlayers,” and playerStats will be the table containing individual player statistics. This should correctly apply the treatments for each player based on the treatment type.

Appreciate the cleanup but still definitely looking for further help on this issue if anyone has noticed anything!

Really stuck on this one unfortunately

Yea it’s giving me a hardtime as well but is it because “stat” is the index and its being used for both exp and coins?