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

DataStore doesn't seem to save my leaderboard?

local DataStoreService = game:GetService("DataStoreService")
local T1 = DataStoreService:GetDataStore("SavePoints")

game.Players.PlayerAdded:Connect(function(Player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = Player
	local Point = Instance.new("IntValue")
	Point.Name = "Points"
	Point.Parent = leaderstats
end)

game.Players.PlayerRemoving:Connect(function(player)
    pcall(function()
        T1:SetAsync(player.UserId.."-Points",player.leaderstats["Points"].Value)
        print("Saved")
    end)
end)

Is anything wrongs with it? because each times i leave and join again it'll reset my points back to 0 ! and not even a single saves in it? btw i already enable Studio Access to API Services

1 comment

0

Didn't work :(

2 answers

The reason is because you never load the stats in after they join.

game.Players.PlayerAdded:Connect(function(Player)
local leaderstats = Instance.new("Folder");
leaderstats.Name = "leaderstats";
leaderstats.Parent = Player;
local info = T1:GetAsync(Player.UserId.."-Points);
local Points = Instance.new("IntValue");
Points.Name = "Points";
Points.Parent = leaderstats;
if(info) then
Points.Value = tonumber(info);
else
T1:SetAsync(Player.UserId.."-Points", 0);
Points.Value = 0;
end
end)

6 comments

0

Didn't work

0

It is a ServerScript right? Make sure it is not a LocalScript, and place it in ServerScriptService.

0

yes it's a Serverscript Script and i'm already put it in the ServerscriptService

0

try changing IntValue to NumberValue.

0

Nvm i just got it worked :D

0

What was wrong with it?

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