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)
The problem with Players.PlayerRemoving
is that it doesn't save the last player in the server, once the last player leaves, the server instantly shuts down. A way to solve that is to use game:BindToClose()
. game:BindToClose()
is a function that allows you to run any code for 30 seconds. That means you better make sure the code runs before the 30 second limit.
--Put this below the PlayerRemoving function (but you can put anywhere else) game:BindToClose(function() for _, v in pairs(game.Players:GetPlayers()) do local success, errorMessage = pcall(function() CurrencySave:SetAsync(v.UserId,v.leaderstats.Fruit.Value) end) if success then print("Data saved and game closed") else warn("Error while saving data: "..errorMessage) end end end)
Also, I realised that you put game.DS:GetService("FirstStore")
. Here's how it's properly spelled:
local DS = game:GetService("DataStoreService") local CurrencySave = DS:GetDataStore("FirstStore") --Replace line 2 with this code
(Note: Do not use the 2nd parameter of Instance.new() as it is deprecated. use leaderstats.Parent
and cash.Parent
instead.)