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

How should I go on to attack a Data Service Storage setup?

Hello, I am a very new scripter (Only 2 days now), and I am looking for a method on what would be the best course of action on how to create a DataServiceStorage for my game. From what I have learned, I should create a script dedicated to saving the buyable models, a seprate script that compiles a bought item from the shop, and saves it to the player, and a seprate script to spawn said saved item. Is this a good general plan for setting this up? I am trying to have all the items from the shop Bought, Sold, and Spawned from a GUI that I have created. Can I please get some feedback?

1 answer

On my YouTube channel M Clan Gaming, I showcase how to use DataStores. Use the following script:

local Players = game.Players
local DSS = game:GetService("DataStoreService")
local DataStore = DSS:GetDataStore("MyDataStore")

Players.PlayerAdded:Connect(function(player)
	local playerUserId = player.UserId --Something to remember the player by
	local data

	local success,errormessage = pcall(function() --Use a pcall so script wont crash if save fails
		DataStore:GetAsync(playerUserId) --Get their data using their access code we wrote
	end)

	if success and data then
		--Do something with data
		print("Got data succesfully!")
	else if success then
		--Do something since they have no data
		print("No data found.")
	else
		warn("There was an error. Error code: " .. errormessage)
	end
end)

Players.PlayerRemoving:Connect(function()
	local playerUserId = player.UserId --Something to remember the player by
	local data = {value1,value2,value3}

	local success,errormessage = pcall(function()
		DataStore:SetAsync(playerUserId,data)
	end)

	if success and data then
		print("Got data succesfully!")
	else if success then
		print("No data found.")
	else
		warn("There was an error. Error code: " .. errormessage)
	end
end)

This is a simple way to save data in a table. We would use data.Money to access that data. Remember, DataStores are pretty advanced scripting for a beginner. Tell me if this has any issues.

Hope this helped!

3 comments

0

Don't get into any big projects if you are still learning. Take some time to learn Lua so you don't get stuck.

0

I understand most of what you had posted. But I am confused on (Value1, Value2, Value3) I understand, if you had a local value for leaderstats earlier in the script, you could save stuff like currency in game, but your script does not hold any local values, is that just a general save script? I also understand that player.UserId is the player.

0

I also have a question about ReplicatedStorage, could I Theoretically use (Value1, Value2, Value3) to be able to save models, or keep modified versions of the original model saved through this? Or would I need to create a new script for that kind of stuff? I know that when you just clone objects through the cloning process, it just creates a copy of the original, that is in replicated storage.

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