Why does the value in my table not change with a for loop?

Hey there! I’ve been trying to make a system where you can pick up a weapon, which will then go into the first available slot. The problem is, is that the value in the table I used won’t change when I use a for loop.

Here’s the script:

local slots = {"None", "None", "None"}

for i, c in pairs(slots) do -- I go through the slots
	if c == "None" then -- I check if c has something
		c = part.Parent.Name -- If it doesn't I overwrite "None" with the weapon name, which does not work. Printing part.Parent.Name will give the correct string.
		break
	end
end

-- Everything works correctly, but slots[1] (c) does not change

Hopefully that is enough info, and any help is greatly appreciated!

Use ipairs rather than pairs. You’re meant to use pairs when iterating through a dictionary, and it looks like slots is an array.

1 Like

u are changing the independent element instead of array element ig

local slots = {"None", "None", "None"}

for i, c in pairs(slots) do 
	if c == "None" then
		slots[i] = part.Parent.Name  
		break
	end
end
1 Like

I used ipairs, but it still doesn’t overwrite Slots[1]. I can see this by doing print(slots[1]) every second.

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