Need help with array

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I’m trying to make a rank up system, just like saber simulator and I think that the best way to do that was to use array.

  2. What is the issue? Include screenshots / videos if possible!
    the issue is that, using index += 1 doesn’t seem to add anything to the leaderstats. When I try to print it, it works perfectly and add + 1 to the index. It just doesn’t show on the leaderstats

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I’ve tried to search on the dev forum for some information but nothing is similar to my problem.

the most important is between the line 1 and 23. the rest is to load the data and save the data (thanks to the dev forum lol)

local dataModule = {}

local rankUpModule = require(game.ReplicatedStorage.ClientServerModules:WaitForChild("RankUpModule"))
local array = {rankUpModule.AllRanks}

for i, v in pairs(array) do
	local index
	index = i
	
	currentRank = v[index].Name
	
	local testforfun = coroutine.create(function()
		while true do
			task.wait(5)
			
			index += 1
			
			print(v[index].Name)
		end
	end)
	
	coroutine.resume(testforfun)
end

function dataModule.addInstance(player: Player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local rankValue = Instance.new("StringValue")
	rankValue.Name = "Rank"
	rankValue.Value = currentRank
	rankValue.Parent = leaderstats
	
	local timeValue = Instance.new("IntValue")
	timeValue.Name = "Time"
	timeValue.Value = 0
	timeValue.Parent = leaderstats
	
	local gemValue = Instance.new("IntValue")
	gemValue.Name = "Gems"
	gemValue.Value = 0
	gemValue.Parent = leaderstats
end

function dataModule.saveData(player: Player)
	local dataStoreService = game:GetService("DataStoreService")
	local dataStore = dataStoreService:GetDataStore("DataStore")
	local leaderstatsFolder = player:FindFirstChild("leaderstats")
	
	local Key = player.UserId
	local data = {
		leaderstatsFolder.Time.Value,
		leaderstatsFolder.Gems.Value,
		leaderstatsFolder.Rank.Value,
	}
	
	local success, result
	
	repeat
		success, result = pcall(function()
			dataStore:UpdateAsync(Key, function()
				return data
			end)
		end)
		
		task.wait()
	until success
	
	
	if not success then
		warn("We were unable to save your data, check this for more info: ".. tostring(result))
	elseif success then
		print("Successfully saved the data for ".. player.Name)
	else
		warn("An error occurred, check this for more info: ".. tostring(result))
	end
	
end

function dataModule.loadData(player: Player)
	local players = game:GetService("Players")
	local dataStoreService = game:GetService("DataStoreService")
	local dataStore = dataStoreService:GetDataStore("DataStore")
	
	local Key = player.UserId
	
	local leaderstatsFolder = player:FindFirstChild("leaderstats")
	local timeValue = leaderstatsFolder:FindFirstChild("Time")
	local gemsValue = leaderstatsFolder:FindFirstChild("Gems")
	local rankValue = leaderstatsFolder:FindFirstChild("Rank")
	
	local data = {
		leaderstatsFolder.Time.Value,
		leaderstatsFolder.Gems.Value,
		leaderstatsFolder.Rank.Value,
	}
	
	local success, result 
	
	
	repeat 
		success, result = pcall(function()
			data = dataStore:GetAsync(Key)
		end)
		
		
		task.wait()
	until success or not players:FindFirstChild(player.Name)
	
	if success then
		timeValue.Value = data[1]
		gemsValue.Value = data[2]
		rankValue.Value = data[3]
		print("Successfully loaded the data for " .. player.Name)
	elseif not success then
		warn("We were unable to load your data, check this for more info: " .. tostring(result))
	else
		warn("An error occurred, check this for more info: " .. tostring(result))
	end
end

return dataModule
 

So if I understood this clearly, it ranks you up in the script, but not on the leaderstats?

Yes, it prints out the value but it doesn’t go in the leaderstats.

All i needed to do was to make that, if a player rank up (via a local script), it would need a remoteEvent to make the player actually get the value and then when the player leaves, it saves into his data (Because this script was in a module script).

TLDR; all i needed was to make a remote event. Changing values doesn’t work in a module script.