Leaderstats not showing up

I know this is a frequently asked question but I can’t seem to solve. Why doesn’t my leaderstat show up?
Roblox doesn’t find any error. And how to fix it. I have studio accesing api services checked. Here is the script.

local DataStore2 = require(1936396537)

DataStore2.Combine(“MasterKey”, “Level”, “XP”)

local defaultLevel = 1
local defaultXP = 0

local xpToLevelUp = function(level)
return 100 + level * 5
end

game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new(“Folder”, player); leaderstats.new.Name = “leaderstats”

local levelValue = Instance.new("IntValue", leaderstats); levelValue.Name = "Level"
local xpValue = Instance.new("IntValue", leaderstats); xpValue.Name = "XP"

local levelStore = DataStore2("Level", player)
local xpStore = DataStore2("XP", player)

local function updateLevel(level)
	player.leaderstats.Level.Value = level
end
local function updateXP(xp)
	if xp >= xpToLevelUp(levelStore:Get(defaultLevel)) then
		xpStore:Increment(xpToLevelUp(levelStore:Get(defaultLevel)) * -1)
		levelStore:Increment(1)
	else
		player.leaderstats.XP.Value = xp
	end
end
updateLevel(levelStore:Get(defaultLevel))
updateXP(xpStore:Get(defaultXP))

levelStore:OnUpdate(updateLevel)
xpStore:OnUpdate(updateXP)

end)

Anyways have a great day

First of all, can you fix the script? Half of it is not in the code block.

You just assumed everyone is using data store 2, not to mention just gave out the code without explaining anything. Hard for someone to learn if it’s just handed to them.

instead of leaderstats.new.Name do leaderstats.Name

2 Likes

Instead of leaderstats.new.Name just type leaderstats.Name = “leaderstats”

So it works now but it doesn’t update the leaderstats.
Is there anything wrong with this code?
local DataStore2 = require(1936396537)

while true do
wait(3)

for k, player in pairs(game.Players:GetChildren()) do
	local xpStore = DataStore2("XP", player)
	
	xpStore:Increment(24)
end

end

yeah that part is ok, but I am a bit confused about updateXP function, it seems wrong.

but u should do this for sure:

else
	player.leaderstats.XP.Value = DataStore2("XP", player):Get(0)
end

And where exactly to put it? Tnx for all of the help

see that line: “player.leaderstats.XP.Value = xp” change it to “player.leaderstats.XP.Value = DataStore2(“XP”, player):Get(0)”

1 Like

Thanks for help! It works now!

1 Like