I am Datastoring tables, having a bit of an error using :SetAsync() function. When I use the SetAsync function, it doesn't really set the PlayerData is the Module script for that player. I need help, the output doesnt output any errors.
Module Script:
return { PlayerData = { Coins = 0, Diamonds = 0, Experience = 0, Kills = 0, Level = 0, EXPneeded = 0, } }
Script:
local DataStore = game:GetService("DataStoreService"):GetOrderedDataStore("PlayerDataV1"); local DataModule = require(game.ReplicatedStorage.PlayerData) game.Players.PlayerAdded:Connect(function(plr) for i, v in pairs(DataModule.PlayerData) do DataStore:GetAsync(plr.UserId, v) end end) game.Players.PlayerRemoving:Connect(function(plr) for i, v in pairs(DataModule.PlayerData) do DataStore:SetAsync(plr.UserId, v) -- v; the value of the instances when looped through the table. end end)
GetAsync will attempt to retrieve the stored value, but you aren't doing anything with the value it returns - it doesn't automatically put the data into the table, you have to do that explicitly: DataModule.PlayerData[i] = DataStore:GetAsync(plr.UserId, i)
(note: don't use v
in GetAsync since you want to send the key (which you named 'i'). ex, 'i' might be "Coins" and 'v' might be 0 - you want to say GetAsync(plr.UserId, "Coins")
, not GetAsync(plr.UserId, 0)
!)
However, for efficiency you should save/load the whole table at once, not key-by-key!
Read through https://developer.roblox.com/articles/Saving-Player-Data for a tutorial on how to do this.