You can write your topic however you want, but you need to answer these questions:
-
What do you want to achieve? That my datastore always works
-
What is the issue? It does not always save
-
What solutions have you tried so far? I tried looking but I did not find anything
Code:
local DataStore = game:GetService("DataStoreService")
local DeathStore = DataStore:GetDataStore("Deaths")
local RCStore = DataStore:GetDataStore("RC")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Model", player)
leaderstats.Name = "leaderstats"
local Deaths = Instance.new("IntValue", leaderstats)
Deaths.Name = "Deaths"
local RC = Instance.new("IntValue", leaderstats)
RC.Name = "RC"
pcall(function()
local DeathValue = DeathStore:GetAsync(player.UserId)
local RCValue = RCStore:GetAsync(player.UserId)
player.CharacterAdded:Connect(function(character)
character:WaitForChild("Humanoid").Died:Connect(function()
Deaths.Value = Deaths.Value + 1
end)
end)
if DeathValue then
Deaths.Value = DeathValue
else
Deaths.Value = 0
end
if RCValue then
RC.Value = RCValue
else
RC.Value = 0
end
end)
end)
game.Players.PlayerRemoving:Connect(function(player)
pcall(function()
if player:FindFirstChild("leaderstats") then
if player.leaderstats:FindFirstChild("Deaths") and player.leaderstats.Deaths.Value then
print("Saving...")
DeathStore:SetAsync(player.UserId, player.leaderstats.Deaths.Value)
print("Saved!")
else
print("Saving was not succesfull!")
end
end
if player:FindFirstChild("leaderstats") then
if player.leaderstats:FindFirstChild("RC") and player.leaderstats.RC.Value then
RCStore:SetAsync(player.UserId, player.leaderstats.RC.Value)
end
end
end)
end)
game:BindToClose(function()
wait()
end)
If someone can tell me what the issue is please tell me! Also don’t hesitate to give me feedback!