Unexpected behaviour from method adding values within a dictionary

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

I want to achieve a system that when a player survives an event, they are awarded a point.

  1. What is the issue? Include screenshots / videos if possible!

The players all share the same score, which is the multiplication of the # of players, and # of events finished. If a player is eliminated and removed from the .alive table, it seems that the player is still rewarded a point when an alive player survives an event.

function Statistics:EventSurvived(event)
	for _, player in self.alive do
		self.players[player.UserId].events_survived += 1
		print(self.players)
	end	
	
	print(self.alive, "Survived", event.event_id, self)
end

For each iteration of the for loop, it seems like a point is added to every single child of self.players but I don’t understand how this could be happening.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

The relevant method, :EventSurvived() is only called once at the end of an event.

I’m completely lost. Thank you for any help in advance.

local Statistics = {}
local sss = game:GetService("ServerScriptService")
local Players = game:GetService("Players")

Statistics.current = {}

local default_player_stats = {
	["events_survived"] = 0
}

Statistics.__index = Statistics

function Statistics.new(players)
	local newStats = {}
	setmetatable(newStats, Statistics)
	
	newStats.players = {}
	newStats.alive = players
	
	for _, player in pairs(players) do
		newStats.players[player.UserId] = default_player_stats -- change key back to user_id
	end
	
	Statistics.current = newStats
	
	return newStats
end

function Statistics:EventSurvived(event)
	for _, player in self.alive do
		self.players[player.UserId].events_survived += 1
		print(self.players)
	end	
	
	print(self.alive, "Survived", event.event_id, self)
end

function Statistics:Eliminate(player)
	if table.find(self.alive, player) then
		table.remove(self.alive, table.find(self.alive, player))
	end
end

The output better shows what I am trying to explain:

▼  {
                    [-3] =  ▼  {
                       ["events_survived"] = 1
                    },
                    [-2] =  ▼  {
                       ["events_survived"] = 1
                    },
                    [-1] =  ▼  {
                       ["events_survived"] = 1
                    },
                 }  -  Server - Statistics:43
                 ▼  {
                    [-3] =  ▼  {
                       ["events_survived"] = 2
                    },
                    [-2] =  ▼  {
                       ["events_survived"] = 2
                    },
                    [-1] =  ▼  {
                       ["events_survived"] = 2
                    }
                 }  -  Server - Statistics:43
1 Like

Whats reasons could cause this issue? Every time EventSurvived() is called, each player of players is granted 1 point for every player in alive. So when an event is finished with 3 survivors, players are granted 3 points rather than 1.

When a player is eliminated and no longer is in the alive table, the :EventSurvived() method seems to still be accounting for eliminated players. Players who are not in alive but are in players (so have been eliminated) are still occurring points as the alive players survive events.

function Statistics:EventSurvived(event)
	-- Print the list of alive players for debugging
	print("Alive players: ", self.alive)

	-- Loop over each player in self.players, and only award points to players who are alive
	for userId, stats in pairs(self.players) do
		local player = Players:GetPlayerByUserId(userId)

		-- Only give points to players who are still alive
		if player and table.find(self.alive, player) then
			self.players[userId].events_survived += 1
			print("Point gained by:", player.Name)
		end
	end	
end

I know this code is essentially working the same and so still does not work but please someone help me understand why EACH player in players is awarded x points where x is the count of players in alive. I really need help on this one

local default_player_stats = {
	["events_survived"] = 0
}

Here lay my issue

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.