How do I make Table stats?

So leaderstats don’t always work so I’m trying to figure out how to make leaderstats from a table. Could anyone help me?

1 Like

Do you still want it to appear in the leaderboard? If yes, you will need to do the steps to add leaderstats or create your own leaderboard.

No it’s just that some people get confused when others say “stats” and not “leaderstats.” So I’m just trying to make regular stats.

Ok. You can do this by attaching a player’s IntValue (which will be the stat) to a Changed event. When it is fired, you will change the player’s stats in a table. It can be something like this:

local stats = {}
game.Players.PlayerAdded:Connect(function(plr)
    local aRandomStat = Instance.new("IntValue")
    aRandomStat.Parent = plr
    stats[plr.UserId]["StateName"] = 0
    aRandomStat.Changed:Connect(function(prop)
        if prop == "Value" then
            stats[plr.UserId]["StatName"] = aRandomStat.Value
        end
    end)
end)
1 Like

or you could just attach the actual object. Seems more reasonable instead of having a bunch of changed connections.

local PlayerStats = {}

local function LoadStats(Player)
	
	local StatFolder = Instance.new("Folder")
	StatFolder.Name = "Stats" -- Name this leaderstats to show it on the Player List
	StatFolder.Parent = Player
	
	local Cash = Instance.new("IntValue")
	Cash.Name = "Cash"
	Cash.Parent = StatFolder
	
	PlayerStats[Player.UserId][Cash.Name] = Cash
end

game.Players.PlayerAdded:Connect(function(Player)
	if not PlayerStats[Player.UserId] then
		PlayerStats[Player.UserId] = {}
	end
	LoadStats(Player)
end)

I also want to mention the usage of .Changed on a IntValue will only update whenever the .Value of said object changes so the if prop == “Value” is not needed.

The Changed event fires whenever the IntValue.Value of the IntValue is changed. It will run with the new value being stored in the argument object, instead of a string representing the property being changed.

3 Likes

Thanks! but this doesn’t work with booleans.

What do you mean? Just create a BoolValue?

local CanJump = Instance.new("BoolValue")
CanJump.Name = "CanJump"
CanJump.Parent = StatFolder

PlayerStats[Player.UserId][CanJump.Name] = CanJump

Test it your own game and it will error.

Just tested it and it works? What code are you using?

local PlayerStats = {}

local function LoadStats(Player)
	
	local StatFolder = Instance.new("Folder")
	StatFolder.Name = "Stats" -- Name this leaderstats to show it on the Player List
	StatFolder.Parent = Player
	
	local TestValue = Instance.new("BoolValue")
	TestValue.Name = "Cash"
	TestValue.Parent = StatFolder
	
	PlayerStats[Player.UserId][TestValue.Name] = TestValue
	
	while wait(1) do
		print(TestValue.Value)
	end
end

game.Players.PlayerAdded:Connect(function(Player)
	if not PlayerStats[Player.UserId] then
		PlayerStats[Player.UserId] = {}
	end
	LoadStats(Player)
end)

the original one you showed. 30 chars

Yes but can you send it? Because it should be working?

im using the exact one you showed just with a boolean named “Job”

Yes but can you send the Code? If you are straight up using the code I sent there should be no error because I literally just tested it and it works as intended. And there is no reason for it to not work with a BoolValue because it is the exact same thing as using a “IntValue” but it uses a Bool instead of a Integer.

nevermind i fixed it. It was a very small problem.

1 Like

Great. What was the problem? :slight_smile:

The name. I messed up the name.

Ah gotcha. :slightly_smiling_face: Hope I managed to help.

I only attach a Changed event to:

  1. To show to the player its stat
  2. To change

Yes but it’s pointless? You can instead just attach the actual stat to the table and then use .Value on the index of the table. Having several .changed for stats is super inefficient + looks bad in code.

If he wants to access it, he can use an InstanceValue or a module script. Either way, it will work. There is nothing pointless in what you do, even in programming.