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

How should I save a BASIC number value? I am having a real hell with this...

So, I am trying to create a script that will save, update and load the number of "Special" rocks value ( Number value).

I just can't understand how to save things...

The script is inside NumberValue.

This is what I've created so far and I know that it's a total fail. -_-

Please take some time to help me.

-- Saves and updates the value

local Datastore = game:GetService("DataStoreService"):GetDataStore("SpecialRockValue")

game.Players.PlayerAdded:Connect(function(player)
	local Value = script.Parent
	pcall(function()
		local specialrocks = Datastore:GetAsync(player.UserId, Value)
	end)
	local Save = Datastore:SetASync(player.UserId, Value)
	
	if Save then
		Save.Value = Value.Value
	end
end)

EDIT: I just want to say that I've also read every catalog about Datastore on ROBLOX Wiki but still no help.

2 answers

✓ Accepted answer

I commented on most lines that have importance in the script, if you have any questions let me know. What this does is loads the saved data if they have it, or saves it when they leave.

local DS = game:GetService('DataStoreService'):GetDataStore('SpecialRockValue')

game:GetService('Players').PlayerAdded:Connect(function(plr)
	local SpecialRocks = Instance.new('IntValue')
	SpecialRocks.Parent = plr
	SpecialRocks.Name = 'SpecialRocks'

	local key = 'id-'..plr.UserId -- get the UserId for the key (better than username as people can change their name)

	local getSavedData
	pcall(function() -- Use pcall in case the retrieval fails
		getSavedData = DS:GetAsync(key) -- get saved data using the key(if there is any)
	end)
	
	
	if getSavedData then -- if there is saved data then
		SpecialRocks.Value = getSavedData[1] -- load saved data
	else
		local ValuesToSave = {SpecialRocks.Value} -- table for values you want to save
		DS:SetAsync(key,ValuesToSave) -- save all the values in the table
	end
end)

game:GetService('Players').PlayerRemoving:Connect(function(plr)
	local key = 'id-'..plr.UserId -- get the userId for the key
	
	local ValuesToSave = {plr.SpecialRocks.Value} -- table for values you want to save
	DS:SetAsync(key,ValuesToSave) -- save all values in table
end)

8 comments

0

userId is deprecated. So is the Instance.new parent argument. You should also save the number itself not the table. It's pointless.

0

Thanks for the pointers, incapaz. But, I was saving the table incase he wanted to add more values into it.

0

I edited it a bit but still thanks for the answer

0

Um

0

Is it not working now?

0

Oh my bad I fixed the problem

0

Alright Ill try it tommorow cuz its night to me now

0

Alright

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