I’m trying to make a kills leaderboard that saves (With Datastore), but I must ask, am I doing it right? I just mashed a bunch of tutorials I watched together to make this script.
local datastoreService = game:GetService("DataStoreService")
local runService = game:GetService("RunService")
local playersService = game:GetService("Players")
local getDatastore = datastoreService:GetDataStore("Kills")
playersService.PlayerAdded:Connect(function(getPlayer)
local getLeaderstats = Instance.new("Folder")
getLeaderstats.Name = "leaderstats"
getLeaderstats.Parent = getPlayer
local getKillsValue = Instance.new("StringValue")
getKillsValue.Name = "Kills"
getKillsValue.Value = getDatastore:GetAsync(getPlayer.UserId) or 0
getKillsValue.Parent = getLeaderstats
getPlayer.CharacterAdded:Connect(function(getCharacter)
local getHumanoid = getCharacter:FindFirstChild("Humanoid")
getHumanoid.Died:Connect(function(getDied)
local getTag = getHumanoid:FindFirstChild("creator")
local getKiller = getTag.Value
if getTag and getKiller then
getKiller.leaderstats.Kills.Value += 1
end
end)
end)
end)
playersService.PlayerRemoving:Connect(function(getPlayer)
getDatastore:SetAsync(getPlayer.UserId, getPlayer:WaitForChild("leaderstats").Kills.Value)
end)
game:BindToClose(function()
task.wait(3)
end)
local datastoreService = game:GetService("DataStoreService")
local runService = game:GetService("RunService")
local playersService = game:GetService("Players")
local getDatastore = datastoreService:GetDataStore("Kills")
playersService.PlayerAdded:Connect(function(getPlayer)
getPlayer.CharacterAdded:Connect(function(getCharacter)
local getHumanoid = getCharacter:FindFirstChild("Humanoid")
getHumanoid.Died:Connect(function(getDied)
local getTag = getHumanoid:FindFirstChild("creator")
local getKiller = getTag.Value
if getTag and getKiller then
getKiller.leaderstats.Kills.Value += 1
end
end)
end)
local getLeaderstats = Instance.new("Folder")
getLeaderstats.Name = "leaderstats"
getLeaderstats.Parent = getPlayer
local getKillsValue = Instance.new("StringValue")
getKillsValue.Name = "Kills"
getKillsValue.Parent = getLeaderstats
local data
local succ, err = pcall(function()
data = getDatastore:GetAsync(getPlayer.UserId)
end)
if succ then
if data then
getKillsValue.Value = data
end
else
warn(err)
end
end)
playersService.PlayerRemoving:Connect(function(getPlayer)
local data
local succ, err = pcall(function()
data = getDatastore:SetAsync(getPlayer.UserId, getPlayer.leaderstats.Kills.Value)
end)
if succ then
if data then
print(data)
end
else
warn(err)
end
end)
game:BindToClose(function()
for _, getPlayer in ipairs(playersService:GetPlayers()) do
local data
local succ, err = pcall(function()
data = getDatastore:SetAsync(getPlayer.UserId, getPlayer.leaderstats.Kills.Value)
end)
if succ then
if data then
print(data)
end
else
warn(err)
end
end
end)
Version with pcall calls added. Just remember that “pcall” yields so it’s best to connect your “CharacterAdded” event before any DataStore loading is performed (as I have done in the above). Other than that your original script was fine.
I joined in a private server with my friend, it works and saves fine but my friend doesn’t have any value? We both tried rejoining but he still doesn’t have anything.
local data
local succ, err = pcall(function()
data = getDatastore:GetAsync(getPlayer.UserId)
end)
if succ then
if data then
if data == nil then
getKillsValue.Value = 0
else
getKillsValue.Value = data
end
end
else
warn(err)
end