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)
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!