How to select all values from table and assign each value to specific values of another table?

I’m trying to figure if I can select all values and set it to another value

like for value1 it will go to othervalue1
and value2 will go to othervalue2
and so on

My code works but It sometimes break because of exhaust time
the code selects the values 1 by 1 and set it to the othervalues

I think a for loop might work but I don’t know how to code it

local rep = game.ReplicatedStorage

local boughtHat = rep.BoughtHat

local bboughtHat =  {
	boughtHat.Hat1,
	boughtHat.Hat2,
	boughtHat.Hat3,
	boughtHat.Hat4,
	boughtHat.Hat5,
	boughtHat.Hat6
}

local boughtGlass = rep.BoughtGlass

local bboughtGlass = {
	boughtGlass.Glass1,
	boughtGlass.Glass2,
	boughtGlass.Glass3,
	boughtGlass.Glass4,
	boughtGlass.Glass5,
	boughtGlass.Glass6
}

local boughtNeck  = rep.BoughtNeck

local bboughtNeck =  {
	boughtNeck.Neck1,
	boughtNeck.Neck2,
	boughtNeck.Neck3,
	boughtNeck.Neck4,
	boughtNeck.Neck5,
	boughtNeck.Neck6
}

local BH = plr.AIS.CH:WaitForChild("BH")
local BHats = {
	BH.Hat1,
	BH.Hat2,
	BH.Hat3,
	BH.Hat4,
	BH.Hat5,
	BH.Hat6
}
local BG = plr.AIS.CG:WaitForChild("BG")
local BGlasses = {
	BG.Glass1,
	BG.Glass2,
	BG.Glass3,
	BG.Glass4,
	BG.Glass5,
	BG.Glass6
}
local BN = plr.AIS.CN:WaitForChild("BN")
local BNecks = {
	BN.Neck1,
	BN.Neck2,
	BN.Neck3,
	BN.Neck4,
	BN.Neck5,
	BN.Neck6,
}
local eh = 0

while eh <= 5 do
	if fin1 == false and fin2 == false  and fin3 == false then
		wait(.1)
		eh = eh + 1
		BHats[eh].Value = bboughtHat[eh].Value
		print(eh)
		if eh == 6 then
			fin1 = true
			eh = 0
		end
	elseif fin1 == true and fin2 == false and fin3 == false then
		wait(.1)
		eh = eh + 1
		BGlasses[eh].Value = bboughtGlass[eh].Value
		print(eh)
		if eh == 6 then
			fin2 = true
			eh = 0
		end
	elseif fin1 == true and fin2 == true and fin3 == false then
		wait(.1)
		eh = eh + 1
		BGlasses[eh].Value = bboughtNeck[eh].Value
		print(eh)
		if eh == 6 then
			fin3 = true
			eh = 0
		end
	elseif fin1 == true and fin2 == true and fin3 == true then
		wait(.1)
		eh = 7
	end
end
1 Like

Hi there,

By putting ‘wait()’ solely under the if-statements, the loop will iterate without yielding if all your statements are false.
Yielding at the end of the loop’s scope can fix the ‘exhaust time’ issue and help with redundancy.

while eh <= 5 do
	-- if statements here

	task.wait() -- wait has been superseded by task.wait
end

If I understood it right you are having a table of elements where you want to change their values. In this case I would want to use a for loop to iterate each specific item:

local table = {
	["1"] = 5;
	["2"] = 56;
	["3"] = 66;
	["4"] = 34
}

for index, value in pairs(table) do
	table[index] = value * value
end

You can change the values however at each index however you want and using the resources at your disposal.