Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Leaderstats not showing when I test my game?

This doesn't usually happen, but my leaderstats for my new game isn't showing. Here is the script. I added it to ServerScriptService.

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("MoneyStats")

game.Players.PlayerAdded:Connect(function(Player)
	local Leaderstats = Instance.new("Folder", Player)
	Leaderstats.Name = "leaderstats"
	
	local Gold = Instance.new("IntValue", Leaderstats)
	Gold.Name = "Gold"
	Gold.Value = 0
	local Diamonds = Instance.new("IntValue", Leaderstats)
	Diamonds.Name = "Diamonds"
	Diamonds.Value = 0
	
	local Data = DataStore:GetAsync(Player.UserId)
	if Data then
		Gold.Value = Data.Gold
		Diamonds.Value = Data.Diamonds
	end
end)

game.Players.PlayerRemoving:Connect(function(Player)
	DataStore:SetAsync(Player.UserId, {
		["Gold"] = Player.leaderstats.Gold.Value;
		["Diamonds"] = Player.leaderstats.Diamonds.Value;
	})
end)

1 comment

0

I actually copy and pasted your script in a baseplate and it worked fine. I didn't even have API/HTTP enabled. I would try to figure out if it is just occurring in that place for you. If that's the case, another script might be interfering.

1 answer

That should've worked just fine but I also recommend using an or statement rather than setting the value twice. That way, if it can't retrieve the player's data, it sets is to 0. You can do that like this:

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("MoneyStats")

game.Players.PlayerAdded:Connect(function(Player)
    local Leaderstats = Instance.new("Folder", Player)
    Leaderstats.Name = "leaderstats"

    local Gold = Instance.new("IntValue", Leaderstats)
    Gold.Name = "Gold"
    local Diamonds = Instance.new("IntValue", Leaderstats)
    Diamonds.Name = "Diamonds"
    pcall(function()
	 locall Data = DataStore:GetAsync(Player.UserId)
	 Gold.Value = Data.Gold or 0
	 Diamonds.Value = Data.Diamonds or 0
   end)
   
end)

game.Players.PlayerRemoving:Connect(function(Player)
    DataStore:SetAsync(Player.UserId, {
        ["Gold"] = Player.leaderstats.Gold.Value;
        ["Diamonds"] = Player.leaderstats.Diamonds.Value;
    })
end)

Also make sure API Services are enabled

This discussion is preserved as it was. New answers, comments, and votes are closed.