#Data Won't Save When Player Leaves/Server Stops

25 messages · Page 1 of 1 (latest)

frigid kayak
#

I've put some comments in the code to show where the errors are occuring. Basically, the lines of code that are supposed to be saving the data into a datastore aren't actually saving the data properly. I'm not sure why, seeing as how the only difference between that, and the line that sets the default currency value (which I already know works), is that the number 10 was changed to a variable for the value. I'm not even getting any error messages in the output I also know that the line to save it should be getting run, as I did check it by putting a print both before and after it, which did print into the output. I don't know what the issue is, and I have absolutely zero clue what could be causing this issue.

local datastore = game:GetService("DataStoreService")
local playerdata = datastore:GetDataStore("PlayerCurrency")
local value = nil

game.Players.PlayerAdded:Connect(function(player)
    local previousdata = playerdata:GetAsync(player.UserId)
    
    local currency
    
    if previousdata ~= nil then
        currency = previousdata
    else
        currency = 10
        playerdata:SetAsync(player.UserId, 10)
    end
    
    local currencyValue = Instance.new("NumberValue", player)
    currencyValue.Name = "Currency"
    currencyValue.Value = currency
end)

-- Problem ↓ ----------------------------------------------------------------------------------

game:BindToClose(function()
    for i,player in pairs(game.Players:GetPlayers()) do
        value = player.Currency.Value
        playerdata:SetAsync(player.UserId, value) -- Won't Save -----------------------
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    value = player.Currency.Value
    if Value ~= nil then
        playerdata:SetAsync(player.UserId, value) -- Won't Save -----------------------
    end
end)
fathom reef
#

ur issue

#

is simple

#

value is not Value

frigid kayak
#

the if statement is checking if the value isn't equal to nil. which it isn't.

fathom reef
#

Value

#

it have

#

capital

#

letter

#

it shouldnt be capital

#

letter

#
local datastore = game:GetService("DataStoreService")
local playerdata = datastore:GetDataStore("PlayerCurrency")

game.Players.PlayerAdded:Connect(function(player)
    local previousdata = playerdata:GetAsync(player.UserId)
    
    local currency
    
    if previousdata ~= nil then
        currency = previousdata
    else
        currency = 10
        playerdata:SetAsync(player.UserId, 10)
    end
    
    local currencyValue = Instance.new("NumberValue", player)
    currencyValue.Name = "Currency"
    currencyValue.Value = currency
end)

game:BindToClose(function()
    for i,player in pairs(game.Players:GetPlayers()) do
        local value = player.Currency.Value
        playerdata:SetAsync(player.UserId, value)
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    local value = player.Currency.Value
    if value ~= nil then
        playerdata:SetAsync(player.UserId, value)
    end
end)
frigid kayak
#

well, that wouldn't affect it when the server is shutting down anyway, which is how I've actually been testing it MORE frequently, and that still won't work anyways

fathom reef
#

say wont work

#

u cant do if Value

#

since Value

#

doesnt exist

frigid kayak
#

everywhere BUT that if statement has it without the capital V

#

and besides, I did say in the post, I had tested it with print next to the lines that should be saving the data

#

and the prints both would run, when the data? not saved.

#

I'll still try it with the fixed capitilization though, just incase I might be stupid here.

#

didn't work.