- 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.
- 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.
- 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