Script only duplicated 1 of 3 items?

Help! My script only duplicates 1 of 3 buttons!

layout:
Screenshot 2022-12-12 160641

It warns button removed but I don’t understand why?

local function load(name, plrname)
	local succ,err = pcall(function()
		local plr = game.Players:FindFirstChild(plrname)
	end)
	if err then
		warn("Player Could Not Be Found")
	end
	if succ then
		local plr = game.Players:FindFirstChild(plrname)
		if script.Buttons:FindFirstChild(name) then
			local button = script.Buttons:FindFirstChild(name):Clone()
			button.Parent = plr.PlayerGui:FindFirstChild('SimpleTopbar').Bar
		else
			error("Button Removed")
		end
	end
end

game.Players.PlayerAdded:Connect(function(player)
	script.Assets.SimpleTopbar.Parent = player.PlayerGui
	for index, instance in pairs(script.Buttons:GetDescendants()) do 
		load(instance.Name, player.Name)
	end
end)

It’s because you are looping through the descendants, you need to loop through the children

local function load(instance, plrname)
	local succ,err = pcall(function()
		local plr = game.Players:FindFirstChild(plrname)
	end)
	if err then
		warn("Player Could Not Be Found")
	end
	if succ then
		local plr = game.Players:FindFirstChild(plrname)

			local button =  instance:Clone() -- Clones the button
			button.Parent = plr.PlayerGui:FindFirstChild('SimpleTopbar').Bar
		
	end
end

for i, v in ipairs(script.Buttons:GetChildren()) do 
		load(v, player.Name) -- Change sending the button name and just sends the whole button
	end

You also can just sned the player over to the script not just it’s name

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