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

Data store does not save data?

Asked by 6 years ago

I'm trying to save a player's data, but it does not appear to save. It gives no errors, and also used to work. It just randomly stopped working

local Data = game:GetService("DataStoreService"):GetDataStore("Cash")
game.Players.PlayerAdded:Connect(function(plr)
    local Folder = Instance.new("Folder",game.ServerStorage)
    Folder.Name = plr.Name
    local Cash = Instance.new("IntValue",Folder)
    Cash.Name = "Cash"
    local Levels = Instance.new("IntValue",Folder)
    Levels.Name = "Levels"
    local SavedItems = Data:GetAsync(plr.userId)
    wait(.1)
    if SavedItems then
        Cash.Value = SavedItems.Cash or 0
        Levels.Value = SavedItems.Levels or 0
    else
        Cash.Value = 0
        Levels.Value = 0
    end
end)

game.Players.PlayerRemoving:Connect(function(plr)
    local Saving = {
        ["Cash"] = game.ServerStorage:FindFirstChild(plr.Name).Cash.Value;
        ["Levels"] = game.ServerStorage:FindFirstChild(plr.Name).Levels.Value}
    Data:SetAsync(plr.userId,Saving)
end)
0
do you have API services on? mixgingengerina10 223 — 6y

1 answer

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

This is probably because the player's UserId is a 64-bit integer. GlobalDataStore:SetAsync() requires a string key.

You can convert the UserId to a string using the tostring() function, like this:

Data:SetAsync(tostring(plr.UserId), Saving)

Or, you can concatenate the UserId to a string, like this:

Data:SetAsync("user_" ..plr.UserId, Saving)

#1 is untested. #2 is tested.

Ad

Answer this question