epic the gamer
epic the gamer

Reputation: 43

My data store system isn't working, thought I get a successfully saved message. Roblox

So I made a data store system that saves Silver. I used pcalls and whenever the player leaves I either get no message or just successfully saved, it's weird I never get any errors.

It doesn't work though. I tried doing it in Roblox itself, not just Studio. Does not work. This is a server script in ServerScriptService.

Please help :D

local dataStore = game:GetService("DataStoreService"):GetDataStore("myDataStore")


game.Players.PlayerAdded:Connect(function(plr)
    
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = plr
    
    local silver = Instance.new("IntValue")
    silver.Name = "Silver"
    silver.Parent = leaderstats
    
    local playerUserId = "Player_"..plr.UserId
    
    local data
    local success, errormessage = pcall(function()
        data = dataStore:GetAsync(playerUserId)
    end)
    
    if success then
        silver.Value = data
    end
    

end)


game.Players.PlayerRemoving:Connect(function(plr)
    local playerUserId = "Player_"..plr.UserId
    
    local data = plr.leaderstats.Silver.Value
    
    local success, errormessage = pcall(function()
        dataStore:SetAsync(playerUserId, data)
    end)
    
    if success then
        print("Data successfully saved.")
    else
        print("Error when saving data.")
        warn(errormessage)
    end
end)```

Upvotes: 1

Views: 621

Answers (1)

SetAsync
SetAsync

Reputation: 72

Datastores require a string as a key. You are passing an integer to SetAsync, you will need to convert this to a string using the tostring() function.

Your corrected code should look like this.

local dataStore = game:GetService("DataStoreService"):GetDataStore("myDataStore")


game.Players.PlayerAdded:Connect(function(plr)
    
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = plr
    
    local silver = Instance.new("IntValue")
    silver.Name = "Silver"
    silver.Parent = leaderstats
    
    local playerUserId = "Player_"..plr.UserId
    
    local data
    local success, errormessage = pcall(function()
        data = dataStore:GetAsync(tostring(playerUserId))
    end)
    
    if success then
        silver.Value = data
    end
    

end)


game.Players.PlayerRemoving:Connect(function(plr)
    local playerUserId = "Player_"..plr.UserId
    
    local data = plr.leaderstats.Silver.Value
    
    local success, errormessage = pcall(function()
        dataStore:SetAsync(tostring(playerUserId), data)
    end)
    
    if success then
        print("Data successfully saved.")
    else
        print("Error when saving data.")
        warn(errormessage)
    end
end)

Upvotes: 1

Related Questions