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

DataStore SetAsync works in Studio but not Client?

Asked by 6 years ago

I can't for the love of life figure out why but essentially, I've made a currency system that's designed to save your currency every time its value is changed, and it works great in studio, but not the client, I'm guessing something is wrong with how I'm saving it but I cannot figure it out. Studio Access to API Services is enabled and this is a script in ServerScriptService, can someone please help me with this?

local DataStore =   game:GetService('DataStoreService'):GetDataStore("CurrencyStoreTest5")


game.Players.PlayerAdded:connect(function(player)
    local currency = Instance.new("NumberValue")
    currency.Parent = player
    currency.Name = "CurrencyValue"
 currency.Value = DataStore:GetAsync(player.userId)

player.CurrencyValue.Changed:connect(function()
    print("Saving")
    DataStore:SetAsync(player.userId, currency.Value)
        print("Saved "..currency.Value.." coins")
end)
end)


0
Wouldn't suggest firing a SetAsync every time the value changes. this will make you run out of possible Requests increadibly fast. RubenKan 3615 — 6y
0
Thanks for the tip, Perhaps changing it every 3 times it's changed would help it out. reddust1 0 — 6y
0
You can only set the same key once every 6 seconds max User#5423 17 — 6y
0
Why are you not saving data when they leave the game? User#5423 17 — 6y
0
Not very necessary with my old method of saving with saving every time it changes, but clearly it's a bad method and I will add the one where it saves when you leave reddust1 0 — 6y

1 answer

Log in to vote
0
Answered by
JellyYn 70
6 years ago

So. I don't want to have to type this out again, but I will show you how I did mine. (Make sure FilteringEnabled is on, because mine works properly in studio and in game.) Sorry if this doesn't work, but it did for me. Any errors, please comment on this answer

local datastore = game:GetService("DataStoreService")
local ds1 = datastore:GetDataStore("DataStore1")

game.Players.PlayerAdded:Connect(function(player)
    local leader = Instance.new("Folder", player)
    leader.Name = "leaderstats"

    local box = Instance.new("IntValue", leader)
    box.Name = "Boxes"
    box.Value = ds1:GetAsync(player.UserId) or 0

    local lvl = Instance.new("IntValue", leader)
    lvl.Name = "Level"
    lvl.Value = ds1:GetAsync(player.UserId) or 0

    ds1:SetAsync(player.UserId, lvl.Value, box.Value)

    box.Changed:Connect(function()
        print("Saving. . .")
        ds1:SetAsync(player.UserId, box.Value)
        print(player.Name.. "'s data of"..box.Value.." "..box.Name.." has been saved!")
    end)
end)
wait()
game.Players.PlayerRemoving:Connect(function(player)
    print("Saving. . .")
    ds1:SetAsync(player.UserId, player.leaderstats.Level.Value, player.leaderstats.Boxes.Value)
    print(player.Name.. "'s data of"..box.Value.." "..box.Name.." has been saved!")
end)

Try doing something with PlayerRemoving then do :SetAsync() inside of that function, like I did. This will make it work properly, as it did for me.

Ad

Answer this question