Hello everyone. I have already made an article on this but i am struggling to save the player’s data after adding cash to the value. I have this script here made by @alphajpeg:
–Server Script
local ReplicatedStorage = game:GetService(“ReplicatedStorage”)
local DatastoreService = game:GetService(“DataStoreService”)
local Datastore = DatastoreService:GetDataStore(“Money”)
local AddMoneyRemote = ReplicatedStorage:WaitForChild(“AddMoney”)
local CreateLeaderstats = function(Player, Data)
local Leaderstats = Instance.new(“Folder”)
Leaderstats.Name = “leaderstats”
Leaderstats.Parent = Player
local Money = Instance.new(“NumberValue”)
Money.Name = “Money”
Money.Parent = Leaderstats
Money.Value = Data.Money
end
game.Players.PlayerAdded:Connect(function(Player)
local Success, Data = pcall(function() --Use pcall for no data loss, error provention, etc
return Datastore:GetAsync(Player.UserId)
end)
if Success then
if Data then
CreateLeaderstats(Player, Data)
else
local Data = {
["Money"] = 0
}
CreateLeaderstats(Player, Data)
end
else
--Do error stuff.
end
end)
game.Players.PlayerRemoving:Connect(function(Player)
local Data = {
[“Money”] = Player.leaderstats.Money.Value
}
Datastore:SetAsync(Player.UserId, Data)
end)
–Add Money Handler
local RepStorage = game.ReplicatedStorage
local event = RepStorage.AddMoney
event.OnServerEvent:Connect(function(Player)
local leaderstats = Player:WaitForChild(“leaderstats”)
local money = leaderstats:WaitForChild(“Money”)
money.Value = money.Value +10
end)
–LocalScript (Fires the event)
script.Parent.MouseButton1Click:Connect(function()
local button = script.Parent
local event = game.ReplicatedStorage.AddMoney
event:FireServer()
end)
I have fixed the part where it will not add any money but it will not save. Can anyone help?
Thanks,
Kieran