Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Data Store Error [plz help?] [Repost]

Asked by 4 years ago
Edited 4 years ago

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!

01local DS = game:GetService("DataStoreService")
02local CurrencySave = game.DS:GetService("FirstStore")
03 
04 
05game.Players.PlayerAdded:Connect(function(player)
06 
07    local leaderstats = Instance.new("Folder", player)
08    leaderstats.Name = "leaderstats"
09    leaderstats.Parent = player
10 
11    local cash = Instance.new("IntValue", leaderstats)
12    cash.Name = "Tags!"
13    cash.Value = 100
14 
15    local data
View all 41 lines...

Code im currently using

Output: attempt to index nil with 'UserId'

01local DS = game:GetService("DataStoreService")
02local CurrencySave = DS:GetDataStore("FirstStore")
03 
04 
05game.Players.PlayerAdded:Connect(function(player)
06 
07    local leaderstats = Instance.new("Folder", player)
08    leaderstats.Name = "leaderstats"
09    leaderstats.Parent = player
10 
11    local cash = Instance.new("IntValue", leaderstats)
12    cash.Name = "Tags"
13    cash.Parent = leaderstats
14    cash.Value = 0
15 
View all 55 lines...

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)

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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.

01--Put this below the PlayerRemoving function (but you can put anywhere else)
02 
03game:BindToClose(function()
04    for _, v in pairs(game.Players:GetPlayers()) do
05        local success, errorMessage = pcall(function()
06            CurrencySave:SetAsync(v.UserId,v.leaderstats.Fruit.Value)
07        end)
08        if success then
09            print("Data saved and game closed")
10        else
11            warn("Error while saving data: "..errorMessage)
12        end
13    end
14end)

Also, I realised that you put game.DS:GetService("FirstStore"). Here's how it's properly spelled:

1local DS = game:GetService("DataStoreService")
2local 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.)

0
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. Dovydas1118 1495 — 4y
0
Output: attempt to index nil with 'UserId' BrishedBoomer 29 — 4y
0
I do not mean put the game:BindToClose() below the event. put the game:BindToClose() outside of the playerRemoving event. @BrishedBoomer Dovydas1118 1495 — 4y
0
Still don't work @Dovydas1118 BrishedBoomer 29 — 4y
View all comments (5 more)
0
also line 5 in your answer Fruit?? BrishedBoomer 29 — 4y
0
Yes. Dovydas1118 1495 — 4y
0
Can you help me fix it?? BrishedBoomer 29 — 4y
0
To be honest, I see nothing wrong with your script. I don't know what's wrong with it. Dovydas1118 1495 — 4y
0
watch i find out im banned for life... [Im not banned, just a joke] BrishedBoomer 29 — 4y
Ad

Answer this question