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

Why is string value not allowed in data stores?

Asked by 4 years ago
Edited 4 years ago

I've made multiple data stores before and this has never occurred. I'm making a new one rn and it gives me an error say "ServerScriptService.Saving:31: bad argument #3(string expected, got nil)". Is this even fixable?

game.Players.PlayerRemoving:Connect(function(player)
    local statstorage = game.ReplicatedStorage.PlayerData:FindFirstChild(player.UserId):GetChildren()
    for i =  1, #statstorage do
        local datastore = game:GetService("DataStoreService"):GetOrderedDataStore(statstorage[i].Name)
        datastore:SetAsync(player.UserId, statstorage[i].Value)
    end 
end)    

function Save(player)
    local statstorage  = game.ReplicatedStorage.PlayerData:FindFirstChild(player.UserId):GetChildren()
    for i =  1, #statstorage do
        local datastore = game:GetService("DataStoreService"):GetOrderedDataStore(statstorage[i].Name)
        datastore:SetAsync(player.UserId, statstorage[i].Value)
    end 
end

------------------------------------------------------------------------------

game.Players.PlayerAdded:Connect(function(player)
    spawn(function()
        while true do
            wait(600)
            Save(player)
        end
    end)
    game.ReplicatedStorage.PlayerData:WaitForChild(player.UserId)
    wait(1)
    local stats = game.ReplicatedStorage.PlayerData:WaitForChild(player.UserId):GetChildren()
    for i = 1, #stats do
    local datastore = game:GetService("DataStoreService"):GetOrderedDataStore(stats[i].Name)        
    stats[i].Value = datastore:GetAsync(player.UserId)
    end
end)


0
The error "ServerScriptService.Saving:31: bad argument #3(string expected, got nil)" means that it is expecting a string value but the value that is being passed in is nil. Judging by how the error is talking about this line "stats[i].Value = datastore:GetAsync(player.UserId)" (line 31), it looks like you are setting stats[i].Value before the GetAsync can return anything. Crystalflxme 104 — 4y

Answer this question