Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Datastoring tables not saving the value?

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)


1 comment

0

Also, I test this out by changing the value of the Module script, as it is for NEW players.

1 answer

✓ Accepted answer

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.

3 comments

0

So you're saying to save/load table one by one, and not have SetAsync()/GetAsync() save/load all values at once?

0

Thanks, this works! But I see what you mean, as in whenever I leave the game, it crashes/takes long time to load. So I am not going to save all at once.

0

The idea is to save a single table with many key/value pairs using a single SetAsync call. So long as all the keys are strings (or they're the numbers 1, 2, 3, ...), this will work.

This discussion is preserved as it was. New answers, comments, and votes are closed.