Hi! This is the script I was making earlier today but, I want it to save everytime the player leaves the game.
Script: game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new(“Folder”)
leaderstats.Name = “leaderstats”
leaderstats.Parent = player
local cash = Instance.new("NumberValue")
cash.Name = "Cash"
cash.Value = 0
cash.Parent = leaderstats
player.CharacterAdded:Connect(function(Character)
Character.Humanoid.Died:Connect(function(Died)
local creator = Character.Humanoid:FindFirstChild("creator")
local leaderstats = creator.Value:FindFirstChild("leaderstats")
if creator ~= nil and creator.Value ~= nil then
leaderstats.Cash.Value = leaderstats.Cash.Value + 10
end
end)
end)
end)
– This is it, what I’m trying to do is every time someone kills a player it gives the killer cash but, I want the leaderstats to save and not be resetted when the player joins the game again. I hope someone helps
-- there's no "player" variable in this code, make one, and yes this is an example
local IntValue = player.leaderstats.IntValue
local DataStore = game:GetService("DataStoreService"): GetDataStore("leaderstatsSave")
local Value = DataStore:GetAsync(player.Name)
if Value then
DataStore:UpdateAsync(player.Name, function()
return IntValue.Value
end)
else
DataStore:SetAsync(player.Name, IntValue.Value)
end
-- and to set the value, here
IntValue.Value = Value
DevHub:
(and I don’t really trust pcalls that much, but thanks)
Ive seen other peoples posts on this topic and the video may be useful but I don’t advise copying from a video (Edit: I just watched it and its good), as for @RocketB0ii they have no pcalls in that script so if it has an error the entire thing will break and can result in dataloss.
Here is two of my very basic functions that allows you to easily save and load data.
local DataStoreService = game:GetService("DataStoreService")
function DSS3SetData(Store,Player,Data)
for i = 1,10 do
local S, R = pcall(function()
local NewStore = DataStoreService:GetDataStore(Store)
NewStore:SetAsync(Player,Data)
end)
if S then
print("Set Data")
break
else
print("DSS Error:")
print(R)
wait(2)
end
end
end
function DSS3GetData(Store,Player)
local Result
for i = 1,10 do
local S, R = pcall(function()
local NewStore = DataStoreService:GetDataStore(Store)
Result = NewStore:GetAsync(Player)
end)
if S then
return Result
else
print("DSS Error:")
print(R)
wait(2)
end
end
end
If you want to see the rest of the datastore script you can find it on my recent post:
local DS = game:GetService("DataStoreService"):GetDataStore("SaveMyData")-- Variable, The Save name you can change that
game.Players.PlayerAdded:Connect(function(player)-- When a player joins the game
local leaderstats = Instance.new("Folder",player)-- Inserting a Folder named Leaderstats
leaderstats.Name = "leaderstats" -- Name of the folder
local Currency = Instance.new("IntValue",leaderstats)-- Inserting a Value in the folder
Currency.Name = "Cash" -- Name of the Value
Currency.Value = 0 -- Starting Value
local plrkey = "id_"..player.userId -- Getting the UserId and savefile
local savevalue1 = player.leaderstats.Cash -- SaveValue, What to save
local GetSaved = DS:GetAsync(plrkey) -- Fetching Data
if GetSaved then
savevalue1.Value = GetSaved[1]-- Getting Value/Savefile
else
local NumbersForSaving = {savevalue1.Value} -- Variable
DS:GetAsync(plrkey, NumbersForSaving)-- What to Save
end
end)
game.Players.PlayerRemoving:Connect(function(player)-- When a player leaves the game
DS:SetAsync("id_"..player.userId, {player.leaderstats.Cash.Value})-- Saves these values
end)
To save or load all you would have to do is put the store name as the first parameter and the player id or whatever your saving the file under as the second.
This way you can save your leaderstat and also get it, all you woukd have to do is check for save data when a player joins and save it every now and then as well as when they leave