Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

lua - 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)```
question from:https://stackoverflow.com/questions/65842834/my-data-store-system-isnt-working-thought-i-get-a-successfully-saved-message

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...