Hello everyone, So, I copied code from DataStore and I would like to define a sum of money but my tests don't work, I don't know how to do it.
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
game.Players.PlayerAdded:Connect(function(Player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = Player
local cash = Instance.new("IntValue")
cash.Name = "Cash"
cash.Parent = leaderstats
local data
local success, errormessage = pcall(function()
data = myDataStore:GetAsync(Player.UserId.."-cash")
end)
if success then
cash.Value = data
else
print("There was an error whilst getting your data")
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(Player)
local success, errormessage = pcall(function()
myDataStore:SetAsync(Player.UserId.."-cash", Player.leaderstats.Cash.Value)
end)
if success then
print("Data successfully saved")
else
print("There was an error when saving data")
warn(errormessage)
end
end)
I tryed:
local cash = Instance.new("IntValue")
cash.Name = "Cash"
cash.Parent = leaderstats
cash.Value = 1000
But it doesn't work, help me please

6 comments
Thank you for answering me and yes, I want when a Player enters the game, he has a certain amount of money so as you said I put cash.Value = 1000 But it doesn't work.
Try this: Making line 12 line 13, and putting "cash.Value = 1000" to line 11!
@BryanFehr It doesn't work, I don't understrand why it doesn't work
It may be due to fact that you have already saved 0 cash in datastore that gets overwritten. Change datastore name and test again
@karlo_tr10 I changed datastore name in line 2: local myDataStore = DataStoreService:GetDataStore("myDataStore"), but it doesn't work
Finally, I changed my script and it's work but thank you for you help