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

How can I make my leaderboard save data better?

I noticed that my leaderboard has been having some errors such as giving someone who has never played my game the same points as someone currently in-game. What should I do to prevent this from happening? Right now it is meant to save their points and their current sword.

local datastore = game:GetService("DataStoreService")
local ds1 = datastore:GetDataStore("PointsSaveSystem")
local ds12 = datastore:GetDataStore("SwordSaveSystem")

game.Players.PlayerAdded:connect(function(plr)
	local folder = Instance.new("Folder", plr)
	folder.Name = "leaderstats"
	local Points = Instance.new("IntValue", folder)
	Points.Name = "Points"


	local EquipedSword = Instance.new("IntValue",plr)
	EquipedSword.Name = "EquipedSword"

 
	Points.Value = ds1:GetAsync(plr.UserId) or 0
	EquipedSword.Value = ds12:GetAsync(plr.UserId) or 1

	ds12:SetAsync(plr.UserId, EquipedSword.Value)
	ds1:SetAsync(plr.UserId, Points.Value)

	game.Players.PlayerRemoving:connect(function(plr)
		ds1:SetAsync(plr.UserId, Points.Value)
		ds12:SetAsync(plr.UserId, EquipedSword.Value)
	end)
end)

4 comments

1

You are connecting a new PlayerRemoving event inside the PlayerAdded event. This will mean every time a player joins you add a new save function which will break your game over time.

0

It will also cause problems with saving the data correctly. Move the event outside of the PlayerAdded event

0

go to forums and find my post

0

Thanks @kingdom5 I didn't notice that

1 answer

✓ Accepted answer

Hey there! I can hopefully help you!

So, a few thing i noticed right of the bat are: You have PlayerRemoving inside the PlayerAdded event, you have two datastore variables, which are not needed. You also are setting the Parent of the instance in the Instance.new() function, which can cause lags in large amounts. And finally you aren't using pcalls

So first of lets fix problem one:

We are going to remove the PlayerRemoving event from PlayerAdded, we can do this like so:

local datastore = game:GetService("DataStoreService")
local ds1 = datastore:GetDataStore("PointsSaveSystem")
local ds12 = datastore:GetDataStore("SwordSaveSystem")

game.Players.PlayerAdded:connect(function(plr)
    local folder = Instance.new("Folder", plr)
    folder.Name = "leaderstats"
    local Points = Instance.new("IntValue", folder)
    Points.Name = "Points"


    local EquipedSword = Instance.new("IntValue",plr)
    EquipedSword.Name = "EquipedSword"


    Points.Value = ds1:GetAsync(plr.UserId) or 0
    EquipedSword.Value = ds12:GetAsync(plr.UserId) or 1

    ds12:SetAsync(plr.UserId, EquipedSword.Value)
    ds1:SetAsync(plr.UserId, Points.Value)


end)

game.Players.PlayerRemoving:connect(function(plr)
	ds1:SetAsync(plr.UserId, plr.leaderstats.Points.Value)
	ds12:SetAsync(plr.UserId, plr.EquipedSword.Value)
end)

Now to fix the second problem we do something like this:

local datastore = game:GetService("DataStoreService")
local ds1 = datastore:GetDataStore("PointsSaveSystem")

game.Players.PlayerAdded:connect(function(plr)
    local folder = Instance.new("Folder", plr)
    folder.Name = "leaderstats"
    local Points = Instance.new("IntValue", folder)
    Points.Name = "Points"


    local EquipedSword = Instance.new("IntValue",plr)
    EquipedSword.Name = "EquipedSword"


    Points.Value = ds1:GetAsync(plr.UserId) or 0
    EquipedSword.Value = ds12:GetAsync(plr.UserId) or 1

    ds1:SetAsync("sword-"..plr.UserId, EquipedSword.Value)
    ds1:SetAsync("points-"..plr.UserId, Points.Value)


end)

game.Players.PlayerRemoving:connect(function(plr)
	ds1:SetAsync("points-"..plr.UserId, plr.leaderstats.Points.Value)
	ds1:SetAsync("sword-"plr.UserId, plr.EquipedSword.Value)
end)

Now we fix the third problem:

local datastore = game:GetService("DataStoreService")
local ds1 = datastore:GetDataStore("PointsSaveSystem")

game.Players.PlayerAdded:connect(function(plr)
    local folder = Instance.new("Folder")
    folder.Name = "leaderstats"
    folder.Parent = plr
    local Points = Instance.new("IntValue")
    Points.Name = "Points"
    Points.Parent = folder


    local EquipedSword = Instance.new("IntValue")
    EquipedSword.Name = "EquipedSword"
    EquipedSword.Parent = plr


    Points.Value = ds1:GetAsync(plr.UserId) or 0
    EquipedSword.Value = ds12:GetAsync(plr.UserId) or 1

    ds1:SetAsync("sword-"..plr.UserId, EquipedSword.Value)
    ds1:SetAsync("points-"..plr.UserId, Points.Value)


end)

game.Players.PlayerRemoving:connect(function(plr)
	ds1:SetAsync("points-"..plr.UserId, plr.leaderstats.Points.Value)
	ds1:SetAsync("sword-"plr.UserId, plr.EquipedSword.Value)
end)

And now we fix the final problem:

local datastore = game:GetService("DataStoreService")
local ds1 = datastore:GetDataStore("PointsSaveSystem")

game.Players.PlayerAdded:connect(function(plr)
    local folder = Instance.new("Folder")
    folder.Name = "leaderstats"
    folder.Parent = plr
    local Points = Instance.new("IntValue")
    Points.Name = "Points"
    Points.Parent = folder


    local EquipedSword = Instance.new("IntValue")
    EquipedSword.Name = "EquipedSword"
    EquipedSword.Parent = plr


    local savedsword, savedpoints   

    local success, err = pcall(function()
	    savedsword = ds1:GetAsync("sword-"..plr.UserId, EquipedSword.Value)
	    savedpoints = ds1:GetAsync("points-"..plr.UserId, Points.Value)
    end)

	if success and savedsword and savedpoints then
		EquipedSword.Value = savedsword
		Points.Value = savedpoints
	end
end)

game.Players.PlayerRemoving:connect(function(plr)
	local success, err = pcall(function()
		ds1:SetAsync("points-"..plr.UserId, plr.leaderstats.Points.Value)
		ds1:SetAsync("sword-"plr.UserId, plr.EquipedSword.Value)
	end)
end)

This took me forever to write, if it helped mark this as the answer!

3 comments

0

I would recommend looking at this question for info about pcalls: https://scriptinghelpers.org/questions/39638/how-does-pcall-xpcall-and-ypcall-actually-work

0

Thanks for the help and all the details about the problems!

0

No problem dude!

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