Making viewport for all children in a folder

  1. What do you want to achieve? I wanna make the viewpport for each tower in ReplicatedStorage.Towers

  2. What is the issue? I dont know how to do for every tower in the folder

  3. What solutions have you tried so far? I looked in dev forum but i guess none helped me

Module:

local TowerShop = {
	["Pistoleer"] = {
		["Name"] = "Pistoleer",
		["RequiresLevel"] = false,
		["LevelRequired"] = 0,
		["Price"] = 0
	},
	["Soldier"] = {
		["Name"] = "Soldier",
		["RequiresLevel"] = true,
		["LevelRequired"] = 1,
		["Price"] = 400
	},
	["Shredder"] = {
		["Name"] = "Shredder",
		["RequiresLevel"] = true,
		["LevelRequired"] = 30,
		["Price"] = 4500
	},
	["Mafia"] = {
		["Name"] = "Mafia",
		["RequiresLevel"] = true,
		["LevelRequired"] = 25,
		["Price"] = 1500
	},
	["Beacher"] = {
		["Name"] = "Beacher",
		["RequiresLevel"] = false,
		["IsAExclusiveTower"] = true,
		["Price"] = 1500
	},
	["Zen"] = {
		["Name"] = "Zen",
		["IsAOwnerTower"] = true,
		["RequiresLevel"] = false,
		["LevelRequired"] = 0,
		["PlayerCanObtain"] = false,
		["Price"] = 0
	},
	["Sniper"] = {
		["Name"] = "Sniper",
		["RequiresLevel"] = true,
		["LevelRequired"] = 10,
		["Price"] = 1000
	},
	["Icetower"] = {
		["Name"] = "Icetower",
		["RequiresLevel"] = true,
		["LevelRequired"] = 3,
		["Price"] = 550,
	},
	["Helicopter"] = {
		["Name"] = "Helicopter",
		["RequiresLevel"] = true,
		["LevelRequired"] = 70,
		["Price"] = 5500
	}
} 
return TowerShop

Local script:

local towers = require(ReplicatedStorage:WaitForChild("TowerShop"))

local playerData = {}

local function getItemStatus(itemName)
	if table.find(playerData.EquippedTowers, itemName) then
		return "Equipped"
	elseif table.find(playerData.OwnedTowers, itemName) then
		return "Owned"
	else
		return "Buy"
	end
end

local function updateShopItems()
	for i, tower in pairs(towers) do
		local oldtowertemplate = itemsFrame.TemplateSlot.ViewportFrame.WorldModel:FindFirstChildOfClass("Model")
		local oldcamera = itemsFrame.TemplateSlot.ViewportFrame:FindFirstChildOfClass("Camera")
		
		local towertemplate = ReplicatedStorage.Towers:FindFirstChild("Pistoleer"):Clone()
		
		local camera = Instance.new("Camera")
		camera.Parent = itemsFrame.TemplateSlot.ViewportFrame
		itemsFrame.TemplateSlot.ViewportFrame.CurrentCamera = camera
		camera.CFrame = CFrame.new(towertemplate.PrimaryPart.Position + towertemplate.PrimaryPart.CFrame.LookVector * 1, towertemplate.PrimaryPart.Position)
		camera.CFrame = towertemplate.PrimaryPart.CFrame * CFrame.Angles(math.rad(10), math.rad(200), 0) * CFrame.new(0, 0, 2)
		
		
		local oldButton = itemsFrame:FindFirstChild(tower.Name)
		
		if oldButton then
			oldButton:Destroy()
		end
		
		local newButton = itemsFrame.TemplateSlot:Clone()
		newButton.Name = tower.Name
		newButton.TowerName.Text = tower.Name
		newButton.Visible = true
		towertemplate.Parent = newButton.ViewportFrame.WorldModel
		newButton.Parent = itemsFrame
		playAnimation(towertemplate, "Idle", "Idle")
		
		local towerStatus = getItemStatus(tower.Name)
		
		if towerStatus == "Buy" then
			gui.Shop.Main.Info.TowerInfo.BuyTowerFrame.Button.BackgroundColor3 = Color3.fromRGB(0, 255, 0)
			gui.Shop.Main.Info.TowerInfo.BuyTowerFrame.Button.MainUIStroke.Color = Color3.fromRGB(0, 0, 0)
			gui.Shop.Main.Info.TowerInfo.BuyTowerFrame.Button.Text = "Buy: $" .. tower.Price
		elseif towerStatus == "Equipped" then
			gui.Shop.Main.Info.TowerInfo.BuyTowerFrame.Button.BackgroundColor3 = Color3.fromRGB(175, 175, 175)
			gui.Shop.Main.Info.TowerInfo.BuyTowerFrame.Button.MainUIStroke.Color = Color3.fromRGB(145, 145, 145)
			gui.Shop.Main.Info.TowerInfo.BuyTowerFrame.Button.Text = "Equipped"
		elseif towerStatus == "Owned" then
			gui.Shop.Main.Info.TowerInfo.BuyTowerFrame.Button.BackgroundColor3 = Color3.fromRGB(255, 140, 0)
			gui.Shop.Main.Info.TowerInfo.BuyTowerFrame.Button.MainUIStroke.Color = Color3.fromRGB(198, 92, 0)
			gui.Shop.Main.Info.TowerInfo.BuyTowerFrame.Button.Text = "Equip"
		end
	end
end

Is your code not already doing that? I skimmed thru and everything seems to be fine and probably would work

Hello, If im not wrong the name can also be the iteration number, in this case (i),I tried something with this script, and it printed ā€œeā€, so try with the iterator i.

local e = {[ā€œeā€] = 1}

for i,c in e do
print(i)
end

this was the script which printed me the string ā€œeā€

1 Like

local towertemplate = ReplicatedStorage.Towers:FindFirstChild("Pistoleer"):Clone() it only supports one model
Captura de pantalla 2024-08-06 160035

1 Like

Well, I dont know if im understanding well your problem, but can you just fix it by changing the FindFirstChild to the name of the tower being iterated, like this: FindFirstChild(tower.Name)

1 Like

I guess just iterate thru all the towers like this then:

for _, towerObj in pairs(ReplicatedStorage.Towers)
   local towertemplate = towerObj:Clone()
   -- rest of your code
end

Is that what you want? I’m not sure. Maybe you can try what @epaes said

Attempt To Index Nil With Clone()

Maybe thats because you wrote wrong a name or just the tower isnt in replicated storage, if I was you, firstly i would check for all the names being correctly placed and writen in replicated storage, if the error keeps showing, i would add a conditional which checks if the tower.Name is actually on replicated storage, and if not, print the name which isnt