When a player joins the game, the server creates a player object and stores the created table to another ModuleScript
, where it can be accessed from other scripts. Because this object stores a lot of information and I don’t want to make the post extremely long, let’s say the player object stores a reference to the actual Player
, a table property containing a few values.
function playerEntity.new(params)
local self = {}
self.Player = params.Player
self.PlayerValues = {
Health = 200
}
return setmetatable(self, playerEntity)
end
-- This is where the problem arises, I think
-- valuesTable contains a lot more key-value pairs in the actual game; it's why I'm using a for loop instead of just setting them equal one-by-one
function playerEntity:updatePlayerValues(valuesTable)
for key, value in pairs(valuesTable) do
self.PlayerValues[key] = value
end
return true
end
function playerEntity:setup()
print("Hi")
end
The ModuleScript
that stores the player objects is called PlayerList
and looks like this:
local playerList = {}
playerList.Players = {}
function playerList:createNewEntry(playerEntityObject)
if self.Players[playerEntityObject.Player.UserId] == nil then
self.Players[playerEntityObject.Player.UserId] = playerEntityObject
end
end
So to modify the properties of a player object, I would do PlayerList.Players [player.UserId].PlayerValues.Health = 300
or, using the method (which is what I will be using), PlayerList.Players[player.UserId]:updatePlayerValues({ Health = 300 })
, for example.
This is what the server script for player joining would look like:
Players.PlayerAdded:Connect(function(player)
local params = {
Player = player
}
local playerEntity = PlayerEntity.new(params)
playerEntity:setup()
PlayerList:createNewEntry(playerEntity)
-- Add to the list so we can access it for later
end)
Now, if I were to call updatePlayerValues
from this server script (in my case, it’s when a RemoteFunction
is invoked), to update a single player’s Health
value, it instead updates all the players’ Health
values.
-- This is in the same server script as the PlayerAdded connection
RemoteFunction.OnServerInvoke = function(player, healthValue) -- Say this is 400
PlayerList.Players[player.UserId]:updatePlayerValues({ Health = healthValue })
-- All the players will have their Health changed to 400
for userId, info in pairs(PlayerList.Players) do
print(info.PlayerValues.Health, userId)
-- This would print 400 for all the players
end
return true
end
I honestly have no idea how any of this is even happening, and I’ve checked for hours to no avail. Help would be greatly appreciated. Please ask if any part isn’t clear enough.