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

Can't save value with datastore ?

Asked by 5 years ago
Edited 5 years ago
local DS = game:GetService("DataStoreService"):GetDataStore("Money")

game.Players.PlayerAdded:connect(function(P)
    local L = Instance.new("Folder",P)
        L.Name = "leaderstats"

    local M = Instance.new("IntValue",L)
        M.Name = "Money"
        M.Value = DS:GetAsync(P.UserId) or 0
end)

game.Players.PlayerRemoving:connect(function(P)
    DS:SetAsync(P.UserId, P.leaderstats.Money.Value)
end)

When i leave it do not save i think i'm using the SetAsync method wrong but the wiki says i should do that http://wiki.roblox.com/index.php?title=Data_store

0
When the server closes PlayerRemoving does not fire so you also need to save data on close. https://wiki.roblox.com/index.php?title=API:Class/DataModel/OnClose User#5423 17 — 5y

2 answers

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

No you are using it correctly, However you shouldn't save your datastore under playeruserid alone. Add something to the end of player userid. This an old code of mine for a DataStore very similar to yours.

local STOREDATAPLZ = {}
local storage = game:GetService("DataStoreService"):GetDataStore("Test")

game.Players.PlayerAdded:connect(function(player)
    local Stats = Instance.new("StringValue", player)
    Stats.Name = "leaderstats"

    local test = Instance.new("NumberValue", Stats)
    test.Name = "test"
    test.Value  = storage:GetAsync(player.userId.."-test") or 0
test.Changed:connect(function()
storage:SetAsync(player.userId.."-test", test.Value)
print("Stats saved :)")
end)
end)
game.Players.PlayerRemoving:connect(function(plr)
    local testValue = plr.leaderstats.test
    storage:SetAsync(plr.userId.."-test", testValue.Value)
    print ("Data saved!")
end)

return STOREDATAPLZ

Notice how I added "-test" to the end of the player.userId so that the DataStoreService could find the specific number.

Edit: Also, the PlayerRemoving might not work on studio even if you have API Services activated, I remember when I wrote that script it did not, so I added the .Changed function

Ad
Log in to vote
1
Answered by 5 years ago

I don't really see anything wrong with your current script in case of using GetAsync and SetAsync, however you should use pcall with data stores if you don't want anything to break.

This is a good link explaining how to use it correctly and it should help you with your problem.

Also parent argument with Instance.new is deprecated so you should assign the parent later.

One last thing is that it has to be a server script, not local so just in case if you used a local script change it to a server script.

Answer this question