How Can I Simplify the loading and display of data from DataStores?

Hello everyone. I would really like to simplify and streamline the code I have written for my XP/stats system. Currently, it seems quite redundant but I’m not sure of any way to simplify this code. Does anybody know a way to simplify this or make it more concise? Thanks.

FYI, this is for a minigames style of a game with a hub place and other places for the minigames.
“downfall” is just the name of a minigame within my game.

function loadData(plr)
	local key = plr.UserId .. "_stats"
	local success, result = pcall(function()
		return data:GetAsync(key)
	end)
	
	if success and result then
		plr.Stats.Level.Value = result[1]
		plr.Stats.XP.Value = result[2]
		plr.Stats.Time.Value = result[3]
		plr.Stats.Games.Value = result[4]
		plr.Stats.Wins.Value = result[5]
		plr.Stats.WinStreak.Value = result[6]
		plr.Stats.Login.Value = result[7]
		plr.Stats.LoginStreak.Value = result[8]
		plr.Stats.Coins.Value = result[9]
		
		plr.leaderstats.Level.Value = result[1]
		plr.leaderstats["Win Streak"].Value = result[6]
		plr.leaderstats.Wins.Value = result[5]
		
		plr.Stats.Downfall.Time.Value = result[10]
		plr.Stats.Downfall.Games.Value = result[11]
		plr.Stats.Downfall.Wins.Value = result [12]
		plr.Stats.Downfall.WinStreak.Value = result[13]
	end
end

function onPlayerAdded(plr)
	task.spawn(function()
		local player = plr
		repeat task.wait() until player.Character
		local humanoid = player.Character:FindFirstChild("Humanoid")
		repeat task.wait()
		until humanoid.Health == 0
		lose(player)
	end)
	
	local folder = Instance.new("Folder")
	folder.Name = "Stats"
	folder.Parent = plr
	
	--general stats
	local levelVal = Instance.new("IntValue")
	levelVal.Name = "Level"
	levelVal.Value = 1
	levelVal.Parent = folder
	
	local xpVal = Instance.new("IntValue")
	xpVal.Name = "XP"
	xpVal.Value = 0
	xpVal.Parent = folder
	
	local timeVal = Instance.new("IntValue")
	timeVal.Name = "Time"
	xpVal.Value = 0
	timeVal.Parent = folder
	
	local gamesVal = Instance.new("IntValue")
	gamesVal.Name = "Games"
	gamesVal.Value = 0
	gamesVal.Parent = folder
	
	local winsVal = Instance.new("IntValue")
	winsVal.Name = "Wins"
	winsVal.Value = 0
	winsVal.Parent = folder
	
	local winStreakVal = Instance.new("IntValue")
	winStreakVal.Name = "WinStreak"
	winStreakVal.Value = 0
	winStreakVal.Parent = folder
	
	local loginVal = Instance.new("IntValue")
	loginVal.Name = "Login"
	loginVal.Value = 0
	loginVal.Parent = folder
	
	local loginStreakVal = Instance.new("IntValue")
	loginStreakVal.Name = "LoginStreak"
	loginStreakVal.Value = 0
	loginStreakVal.Parent = folder
	
	local coinsVal = Instance.new("IntValue")
	coinsVal.Name = "Coins"
	coinsVal.Value = 0
	coinsVal.Parent = folder

	--leaderboard-displayed stats
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr
	
	local leaderLevel = Instance.new("IntValue")
	leaderLevel.Name = "Level"
	leaderLevel.Value = 1
	leaderLevel.Parent = leaderstats
	
	local leaderStreak = Instance.new("IntValue")
	leaderStreak.Name = "Win Streak"
	leaderStreak.Value = 0
	leaderStreak.Parent = leaderstats
	
	local leaderWins = Instance.new("IntValue")
	leaderWins.Name = "Wins"
	leaderWins.Value = 0
	leaderWins.Parent = leaderstats
	
	--downfall stats
	local downfall = Instance.new("Folder")
	downfall.Name = "Downfall"
	downfall.Parent = folder
	
	local downfallTimeVal = Instance.new("IntValue")
	downfallTimeVal.Name = "Time"
	downfallTimeVal.Value = 0
	downfallTimeVal.Parent = downfall
	
	local downfallGamesVal = Instance.new("IntValue")
	downfallGamesVal.Name = "Games"
	downfallGamesVal.Value = 0
	downfallGamesVal.Parent = downfall
	
	local downfallWinsVal = Instance.new("IntValue")
	downfallWinsVal.Name = "Wins"
	downfallWinsVal.Value = 0
	downfallWinsVal.Parent = downfall
	
	local downfallStreakVal = Instance.new("IntValue")
	downfallStreakVal.Name = "WinStreak"
	downfallStreakVal.Value = 0
	downfallStreakVal.Parent = downfall
	
	loadData(plr)
	
	xpVal:GetPropertyChangedSignal("Value"):Connect(function()
		local levelUpXP = math.floor(levelVal.Value ^ 1.5 * 500)
		if xpVal.Value >= levelUpXP then
			levelVal.Value += 1
		end
	end)
end

You can start out by saving your data as a dictionary and not an array. That way you can do result.Level or result["Level"] instead of result[1].

1 Like

ended up just using loleris’ new ProfileStore system instead, don’t really have to worry about any of this anymore. Check it out it’s very helpful. → ProfileStore - Save your player data easy (DataStore Module)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.