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

Confused About Data Stores?

Hey guys, so I've obviously looked at the wiki page here but I can't seem to understand how to use data stores to get character information, or store it.

I have the following code:

local levelManagement = Instance.new("Smoke", game.ServerStorage)
levelManagement.Name = "LevelStorage"

game.Players.PlayerAdded:connect(function(player)
	local levelStorage = Instance.new("NumberValue", game.ServerStorage.LevelStorage)

	levelStorage.Name = player.Name
	levelStorage.Value = 1

	local leaderboard = Instance.new("IntValue")
	leaderboard.Name = "leaderstats"
	leaderboard.Parent = player

	local level = Instance.new("IntValue")
	level.Name = "Level"
	level.Parent = leaderboard
	level.Value = levelStorage.Value
end)

game.Players.PlayerRemoving:connect(function(player)
	local levelStorage = game.ServerStorage.LevelStorage:FindFirstChild(player.Name)

	if (levelStorage) then
		levelStorage:Destroy()
	end
end)

I'm trying to get the "levelStorage.Value" to get data from previously saved information. I'm also trying to have it save information after the person leaves.

I'm not looking for a handout, as I want to learn more than just have someone else finish my work for me. Just point me in the right direction, please?

1 comment

0

Wiki seems to explain it good enough.

1 answer

✓ Accepted answer

To save information use the SetAsync method. Remember to wrap this in a pcall in case it fails. It takes 2 parameters: key and value. So to save to a player, something like this would work:

success,message=pcall(function()
	game:GetService("DataStoreService"):GetDataStore("UserData"):SetAsync(Player.UserId,data)
end)

http://wiki.roblox.com/index.php?title=API:Class/GlobalDataStore/SetAsync http://wiki.roblox.com/index.php?title=Global_namespace/Basic_functions#pcall

to get the data, you would use GetAsync. It takes the key and returns the value saved to it.

success,message=pcall(function()
	savedvalue=game:GetService("DataStoreService"):GetDataStore("UserData"):GetAsync(Player.UserId)
end)

http://wiki.roblox.com/index.php?title=GetAsync

http://wiki.roblox.com/index.php?title=Data_store

call the first and second code when the player leaves the game and joins it, respectively

If you only want to save 1 value, setting it as it is will work fine. If you have several values, convert them to a table before saving them so you only have to make 1 call each time.

Hope this helps you

4 comments

0

Thanks, mate! I really appreciate the help.

0

No problem. Would you mind accepting the answer?

0

The wiki recommends to use UpdateAsync instead of SetAsync to save information. Would this be required at all since it's recommended?

0

@PersonaCode UpdateAsync is only when more than 1 server may try to change it at once. Its bulkier and not good for player data

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