All players in game getting the same class

function dataManager:GetClass(player)
	local user = playersData[player.UserId]
	if not user then return end
	
	print('GetClass', player, user.EquippedClass) -- Prints other players classes)
	
	return user.EquippedClass
end

Ignore all the prints. Problem is that if a player changes their class and reset, they get given that class, but if another player resets, they get given the same class as the other players class.

So basically,
Player1 and Player2 are both Knights. Player1 changes their class to Archer, and reset. They respawn as an archer. Player2 dies/resets and they respawn as an archer, even tho they should still be a Knight.

1 Like

I’d recommend only posting the code relevant to the problem because I just spent 5 minutes reading the first function and wondering how it’s related to your problem.

function dataManager:GetClass(player)
	local user = playersData[player.UserId]
	if not user then return end
	
	print('GetClass', player, user.EquippedClass)
	
	return user.EquippedClass
end

Basically, if Player1 changes their class to Archer and Player2 dies with Knight still equipped, it prints

GetClass Player2 Archer

So for some reason the User.EquippedClass

Think I found the problem, but not sure why it is so.

So testing in studio on 2 player server, printing

user

returns the same same table for both players. Not sure if this is suppose to happen? I’d think that each player would have their own individual table??

You’re not giving enough information. Based on the function you provided, yes, each user should have their own information. But this is assuming at least two things:

  1. You’re storing data for each player separately, and not accidentally tangling them together.
  2. You’re storing each piece of data under the appropriate UserId.

Problems like these are usually extremely simple to fix but difficult to solve because of assumptions being made. Try breakpointing in a few key areas and see what data you’re actually dealing with.

The problem most likely lies where you assign the data to the player when they join.

If you do something like this:

local Data = {
-- lots of content
}
local PlayerData = {}

function PlayerAdded(Player)
    PlayerData[Player.UserId] = Data
end

Then both players will have the same data throughout the whole game. Instead you would need to change it to

local PlayerData = {}

function PlayerAdded(Player)
    PlayerData[Player.UserId] = {
        -- lots of content
    }
end