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

How do i get my Data store script to save/load stats from a table?

Hello, I'm trying to have a re-attempt at my saving script to try make it more efficient. To do this i want to minimise the amount of data storing happening in the servers meaning storing a table under one Data store.

I have heard it is possible and therefore would love to get this working. I have attempted to do this and have failed. If anyone could point me in the right direction i would really appreciate it. Thanks.

Heres a snippet of the code which attempts at saving/loading a table:

local datastore = game:GetService("DataStoreService")

--Saving Variables here
local ds1 = datastore:GetDataStore("Data")



-----------------------------------------------------



game.Players.PlayerAdded:connect(function(plr)
	local folder = Instance.new("Folder", plr) -- Creates a folder in the player
	folder.Name = "Data" -- Having the name other than 'leaderstats' will make all values private and therefore other players will not see the stats.
	
	local leaderstat = Instance.new("Folder", plr)
	leaderstat.Name = "leaderstats"
		
		
	--If making a new stat to save, please also define it here.
	--------------------------------------------------------------------
	
	
	local Level = Instance.new("IntValue", folder)-- Creating the Level
	Level.Name = "Level"	
	local Experience = Instance.new("IntValue", folder)-- Creating the Xp 
	Experience.Name = "Xp"
	local Currency = Instance.new("IntValue", folder)-- Creating the Currency
	Currency.Name = "DefenderCoins"
	
	local Dir = {Level.Value,Experience.Value,Currency.Value} 
			
	---------------------------------------------------------------------	
	
	local Data = plr:FindFirstChild("Data")
	
		
	local data = ds1:GetAsync(plr.UserId) or nil  --Loads in player data then saves however if no data is found then data will be reset to 0
	if data	== nil then
		Data:WaitForChild("Level").Value = 1
		ds1:SetAsync(plr.UserId, Dir)
		data = ds1:GetAsync(plr.UserId) or nil
	end
	
	Level.Value = data[1]
	Experience.Value = data[2]
	Currency.Value = data[3]
	
	
	

	
	
	
	
		
	--[[ --THIS WHILE LOOP IS JUST  FOR TESTING THE SAVE
	while true do wait(20) -- Autosaver						
		ds1:SetAsync(plr.UserId, Dir)			
		print("Data Auto Saved")
	end
	--]]	
	






			
		
		
end)

game.Players.PlayerRemoving:Connect(function(plr)			
	ds1:SetAsync(plr.UserId, {plr.Data.Level.Value,plr.Data.Xp.Value,plr.Data.DefenderCoins.Value})		
	print("Data Saved")
end)	

P.S: Original saving script works but i had edited that script to make this to try save and load a table. No errors pop up so its must be a logic error.

7 comments

1

Now that you've updated it, start the process of debugging code by setting up print'1', 2 and so forth around your code. I'd give the the answer straight up but this is a FM, though you are making an attempt at learning. Set up the print()'s, then see where it stops and go from there! Keep updatingthe question

0

It passes through the whole script. It just doesn't like the table. ANd i've tried printing out the table and the values show 0 even though in test mode i've changed them to try save it. So what i may try to do is manually put the directory for the values instead of using the cloned directories.

0

Thankyou very much for your assistance and for aiding me through it rather than giving me the code. Much appreciated :)

0

The issue was that local test mode still doesn't allow for the player to change data so i had to goo into the local server test mode and change the values via the server. it works ^_^

0

Glad I could help! I thought you were testing this using Test > Start Server, so I was a bit confused heh. Glad you figured out why it wouldn't save after is passed through the whole script! That's part of the process of debugging, and you're getting better at it already. I do prefer to give an outline of the code rather than the answer, to see if you can translate what I said in English into code

0

Also, here's a quick wiki on spawn: https://www.robloxdev.com/articles/Built-in-Functions-and-Variables/Roblox#spawn It, in this case, would allow the while loop to exist, while the code still passing through. I don't think you need it here, though good to know in the future. You can roam the SH site for questions involving spawn()

0

Thats convenient. So essentially 2 sections of the code is running at once. I'll keep that in mind :)

1 answer

✓ Accepted answer

I'll give you the logic behind this, rather than the code. Of course if you still need help, just ask. Here's how the code should work:

  • Player joins the game
  • Leaderstats are made
  • Script checks if the player has saved data. If it doesn't (if conditional statement), create the data as {1, 0.0} (or use Dir, whichever is your preference), immediately save it to the player's data. >Set the data variable to equal the new saved data (which is {1, 0,0})
  • Outside of the if conditional statement: set the leaderstats to data[1], data[2], and data[3] respectively.
  • Move the PlayerRemoving event/connection line outside of the PlayerAdded event
  • Maybe put the while loop in a spawn() function, not sure if required though.

4 comments

0

How would i use an if statement to check if there is valid data within the data save?

1

Look at what you did in line 12. GetAsync returns the data or nil!

0

and also, the 'spawn() function' i've never heard of it. lol. What does it do?

0

I've tried using your logic and it has not worked. I'll update my post with the new code

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