HELP its been about month my game is delayed cuz the datastore error
link to original Post: https://scriptinghelpers.org/questions/105089/data-store-error-plz-help
Can somebody fix my code to work, im trying to make a DataStore for Cash
Output: DS isn't a vaild part of DataModel
Thanks to anybody who fixes it!
local DS = game:GetService("DataStoreService")
local CurrencySave = game.DS:GetService("FirstStore")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local cash = Instance.new("IntValue", leaderstats)
cash.Name = "Tags!"
cash.Value = 100
local data
local success, errormessage = pcall(function(player)
data:GetAsync(player.UserId.."-cash")
end)
if success then
cash.Value = data
else
print("There was an error while getting your data")
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall (function()
CurrencySave:SetAsync(player.UserId.."-cash", player.leaderstats.cash.Value)
end)
if success then
print("Player data Saved!")
else
print("Player Data didn't Save.")
warn(errormessage)
end
end)
Code im currently using
Output: attempt to index nil with 'UserId'
local DS = game:GetService("DataStoreService")
local CurrencySave = DS:GetDataStore("FirstStore")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local cash = Instance.new("IntValue", leaderstats)
cash.Name = "Tags"
cash.Parent = leaderstats
cash.Value = 0
local data
local success, errormessage = pcall(function(player)
data:GetAsync(player.UserId.."-cash")
end)
if success then
cash.Value = data
else
print("There was an error while getting your data")
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
game:BindToClose(function()
for _, v in pairs(game.Players:GetPlayers()) do
local success, errorMessage = pcall(function()
CurrencySave:SetAsync(v.UserId,v.leaderstats.cash.Value)
end)
if success then
print("Data saved and game closed")
else
warn("Error while saving data: "..errorMessage)
end
end
end)
local success, errormessage = pcall (function()
CurrencySave:SetAsync(player.UserId.."-cash", player.leaderstats.cash.Value)
end)
if success then
print("Player data Saved!")
else
print("Player Data didn't Save.")
warn(errormessage)
end
end)
the output is the same but here,
Output: attempt to index nil with 'UserId' but once i click on the error it brings me to line 25,
warn(errormessage)

9 comments
I edited the answer because I see the error with your code. DataModel is another word for game. Even if the error is fixed, it won't save your data, so I put game:BindToClose() as the thing.
Output: attempt to index nil with 'UserId'
I do not mean put the game:BindToClose() below the event. put the game:BindToClose() outside of the playerRemoving event. @BrishedBoomer
Still don't work @Dovydas1118
also line 5 in your answer Fruit??
Yes.
Can you help me fix it??
To be honest, I see nothing wrong with your script. I don't know what's wrong with it.
watch i find out im banned for life... [Im not banned, just a joke]