Dictionary value resets out of i, v in pairs() loop

  1. What do you want to achieve? I am trying to overwrite a stats dictionary through getting the value of the stat with the same key in the player.

  2. What is the issue? The variables are not changing outside the i, v in pairs() loop (immediately changes back).

    (196 is printed v directly after loop, 193 is printed v in loop with the correct values).

  3. What solutions have you tried so far? I have tried to use roundStats.v (dictionary name), but it breaks the rest of the program without returning any error messages. I have also tried to use devforum search and github, as well as the roblox documentation, but none of them seem to have this error and/or say to set it regularly with =.

Relevant code:

			for i, v in pairs(roundStats) do
				roundStats.v = player.Stats:WaitForChild(tostring(i)).Value
				print(tostring(v))
			end
			for i, v in pairs(roundStats) do
				print(tostring(v))
			end

(maybe) relevant code:

function createValuesOfIn(constructType, valueType, valueTable, valueTable2, valueFolder, parent)
	if constructType == "single" then
		local newValue = Instance.new(valueType)
		newValue.Name = valueTable2
		newValue.Value = valueTable
		newValue.Parent = parent
	else
		local newFolder = Instance.new("Folder")
		newFolder.Name = valueFolder
		newFolder.Parent = parent

		if constructType == "dictionary" then
			for i, v in pairs(valueTable) do
				local value = Instance.new(valueType)
				value.Name = i
				value.Value = v
				value.Parent = newFolder
			end
		elseif constructType == "array" then
			for i, v in valueTable do
				local value = Instance.new(valueType)
				value.Name = valueTable2[i]
				value.Value = v
				value.Parent = newFolder
			end
		else
			print("Please enter a valid value!")
		end
	end
end

createValuesOfIn("dictionary", "IntValue", baseDictionary, "nil", "Stats", player)

image
(all stats are setting correctly under the correct folder)

Thank you for reading this post!

do roundStats[i] = player.Stats:WaitForChild(tostring(i)).Value

Still not working for some reason?

for i, v in pairs(roundStats) do
	local newValue = [path_to_player].Stats:WaitForChild(i).Value -- Get the stat object value and store it
	roundstats[i] = newValue -- set table value as new value (note: 'v' would refer to the current value and thus not change it)
	print('updated '..i..' to value: '..newValue) -- confirm we updated the value
end

i is a string, v is a number,
path_to_player is just meant to refer to whatever variable you use to represent the player object

roundStats = {
	hp = 10,
	sp = 20,
	atk = 5,
	def = 5,
	spatk = 5,
	spdef = 5
}

(sry i forgot to put this in)

based on that, the method i posted should be working

roundStats[i] = somenumber

print(roundStats[i]) or print(v) should == some number

It’s still not working for some reason, is there any other possible reason why it might be broken? (I don’t think i’m resetting the stats either)

for i, v in pairs(roundStats) do
	local newValue = [path_to_player].Stats:WaitForChild(i).Value -- Get the stat object value and store it
	roundstats[i] = newValue -- set table value as new value (note: 'v' would refer to the current value and thus not change it)
	print('updated '..i..' to value: '..newValue) -- confirm we updated the value
end

i is a string, v is a number,
path_to_player is just meant to refer to whatever variable you use to represent the player object

Meant to make this a reply not an edit, but don’t want to delete it so here - sorry for the double bump

Tysm! Could u explain why this method works opposed to a 1 line method?

You can absolutely use a 1 line method, but for the purposes of explaining in comments it made more sense to save it as a variable to be able to compare to more easily. You could also do:

for i, v in pairs(roundStats) do
	roundstats[i] = [path_to_player].Stats:WaitForChild(i).Value 
end

-- edit: fixed extra space

which doesnt save a variable, and thus theoretically is more efficient from a computing standpoint, but it doesnt explain much :slight_smile:

Also, 100% transparency, the entire reason I helped is cause you put a ‘thank you’ in your original post. Sometimes that subtle little stuff makes a difference :+1:

1 Like