So, im currently making a gun game with a friend, and i have a script to reward players at the end of a round, however it only gives half the players rewards, and is very inconsistent.
Ive attempted to fix this myself for hours now, and it just wont work.
Heres my code:
-- Rewards
local TableToSort = {} -- Create a new table to sort the kills by (this works in a different script)
for i, v in pairs(game.Players:GetChildren()) do
if v:FindFirstChild('PlayerData') and v:FindFirstChild('RoundStats') then
local NewPlayer = {PlayerName = v.Name; Kills = v.RoundStats.Kills.Value}
table.insert(TableToSort, NewPlayer) -- add the player to the table
end
end
-- sort the table
table.sort(TableToSort, function(a, b)
return a.Kills > b.Kills
end)
-- reward cash is multiplied by the number of players, and then divided by the placement of the player.
local RewardCash = BaseWinCash*game.Players.NumPlayers
local PlayersTable = {} -- make a new table to send to the clients
for i, v in pairs(TableToSort) do -- for each player
local PlayerRewardCash = (RewardCash / table.find(TableToSort, v)) -- as i said, rewardcash is divided by the placement
game.Players:FindFirstChild(v.PlayerName).PlayerData.PlayerMoney.Value += PlayerRewardCash
print('Gave '..v.PlayerName..' KC$'..PlayerRewardCash..'. Placement: '..table.find(TableToSort, v))
table.insert(PlayersTable, {PlayerName = v.PlayerName, PlayerReCash = PlayerRewardCash})
end
game.ReplicatedStorage.Events.DisplayWinner:FireAllClients(PlayersTable) -- tell the clients to display the final scores, and each players reward cash
Screenshots of the game rewarding only First place, and not second.


