Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Data Store Error [plz help?] [Repost]

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)

1 answer

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.)

9 comments

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.

0

Output: attempt to index nil with 'UserId'

0

I do not mean put the game:BindToClose() below the event. put the game:BindToClose() outside of the playerRemoving event. @BrishedBoomer

0

Still don't work @Dovydas1118

0

also line 5 in your answer Fruit??

0

Yes.

0

Can you help me fix it??

0

To be honest, I see nothing wrong with your script. I don't know what's wrong with it.

0

watch i find out im banned for life... [Im not banned, just a joke]

This discussion is preserved as it was. New answers, comments, and votes are closed.