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

How do you create a DataSaving script only for a Value?

Here is my question: How do I create a DataSaving script for an IntergerValue's Value that has nothing to do with a player?

Data saving is new to me, so I'm lost. I tried to mess with a leaderboard DataSaving script to make it work for just the IntValue, but it didn't turn out right. I'll give you the full leaderboard save script that I used:

local datastore = game:GetService("DataStoreService"):GetDataStore("DataSave0") 


game.Players.PlayerAdded:connect(function(player)
	local leaderstats = Instance.new("IntValue")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local points = Instance.new("IntValue") 
	points.Name = "Points"
	points.Parent = leaderstats
	
	local wins = Instance.new("IntValue")
	wins.Name = "Wins" 
	wins.Parent = leaderstats
	
	local key = "user-" .. player.userId
	
	local storeditems = datastore:GetAsync(key)
	if storeditems then
		points.Value = storeditems[1]
		wins.Value = storeditems[2] 
	else
		local items = {points.Value, wins.Value} 
		datastore:SetAsync(key, items)
	end
end)

game.Players.PlayerRemoving:connect(function(player)
	local items = {player.leaderstats.Points.Value, player.leaderstats.Wins.Value} 
	local key = "user-" .. player.userId
	
	datastore:SetAsync(key, items)
end)

It should probably be fairly easy, but I am still lost.

2 comments

0

Not a bad try, I don't know if it will work lol, but have a look at my answer :P and if you want to save it in a table, do with getasync do or {} (you'll see what I mean, I already explained what to do with bools, that place)

0

Lol yeah Ill have a look

1 answer

✓ Accepted answer

So if I'm understanding you correct, you want a value that the server saves, not the player? I also understand that wiki should do a better job at explaining datastores lol. I'll try to answer your question but you should have a look at this too, I explain there how it works.

So, a key isn't neccesary, but it's always handy to reset datastores as it is not possible to remove them.

A quick explanation, GetAsync() and SetAsync() are the things you really need to understand. Like in this example

ServerSave:SetAsync(key,values)

ServerSave would be your datastore then, key your datastore version and values... your values you want to save.

GetAsync would work the same

local Values = ServerSave:GetAsync(key)

Only difference would be that you don't need to fill in values.

So what I'm trying to say is that the only thing that changes is your KEY, nothing else will change. I'll make a quick example script under here.

-- Server script, your can't save things in local scripts!

local DataStore = game:GetService('DataStore')
local MainStore = DataStore:GetDataStore('MainStore')

local key = 1000 -- THIS VALUE DOESN'T MATTER!
local val = game:GetService('ReplicatedStorage'):WaitForChild('Value') -- Or your values name

wait(2)
local GetVal = MainStore:GetAsync(key) or 0 -- Note, if your value is for example a bool you have to change this to false (or maybe true) the reason I add this is to make sure it doesn't break if it never saved before

game:GetService('ReplicatedStorage'):WaitForChild('Value').Value = GetVal

while wait(60) do -- You can change 60 to the time between saves, I wouldn't recommend making it smaller than 20 though
	MainStore:SetAsync(key, val.Value)
end

If I made any typing mistakes, please say so. Good luck with datastores! (I know they are very hard :/)

2 comments

0

Very Appreciated

0

:P

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