Problem with Tools Giving

Hello!, i have a problem with my tools giving script upon character loading, some times, it only iterates thru the first folder of my toolsfolder and stops, why is that?


game:GetService("Players").PlayerAdded:Connect(function(plr)
	game:GetService("RunService").Heartbeat:Wait()
	--
	local Connections = {
		AncenstryCon = nil,
		CharSpawnedCon = nil
	}
	if _G.EnableStudioStuff and workspace:FindFirstChildWhichIsA("Folder") and not workspace:FindFirstChildWhichIsA("Folder"):GetAttribute("BossEnabled") then
		local MainMenuUI = variablesneeded.gameassets.SideAssets.Guis.UI.MainUI.MainMenuUI:Clone()
		MainMenuUI.Parent = plr.PlayerGui
		--
	local STMenuUI = variablesneeded.gameassets.SideAssets.Guis.UI.MainUI.StartingMenuUI:Clone()
	STMenuUI.Parent = plr.PlayerGui
	end
	-- Char Spawned Connection
	Connections.CharSpawnedCon = plr.CharacterAdded:Connect(function(char)
		if plr:IsDescendantOf(game) and char then
			plr.Backpack:ClearAllChildren()
			-- Making UI Visible once again
			for _, ui in plr.PlayerGui:GetChildren() do
				if ui:IsA("ScreenGui") and ui then
					ui.Enabled = true
				end
			end
			--
			local humanoid = char:WaitForChild("Humanoid")
			--
			if plr.PlayerGui:FindFirstChild("MainMenuUI") then
				task.wait(0.1)
				if plr.PlayerGui:FindFirstChild("MainMenuUI") then
				humanoid.MaxHealth += 5000000
				humanoid.Health += 5000000
				char.PrimaryPart.Anchored = true
				end
			end
			
			if workspace:FindFirstChildWhichIsA("Folder") then
				local ToolsFolder = variablesneeded.gameassets:WaitForChild("MainAssets"):WaitForChild("Tools")
				
				--
				
			for _, toolfolder in variablesneeded.gameassets:WaitForChild("MainAssets"):WaitForChild("Tools"):GetChildren() do
				local randomtool = toolfolder:GetChildren()[math.random(1, #toolfolder:GetChildren())]:Clone()
				randomtool.Parent = plr.Backpack
				
				-- // Checking if the tool of the handle can be animated
				if randomtool:GetAttribute("AnimateHandle") then
					variablesneeded.Modules.GlobalModule.AnimateToolHandles(randomtool)
				end
				--
				local success, result = pcall(function()
						if plr:IsDescendantOf(game) then
							return variablesneeded.gameassets.MainAssets.Remotes.ExternalSupport.ExternalSupportRF:InvokeClient(plr, "checkdevicetype")
						end
				end)
				if randomtool:GetAttribute("NeedsMobileSupport") and result then
					variablesneeded.Modules.ExternalSupportModule.MobileSupportHandler(randomtool, humanoid)
				end
			end
			end
		end
	end)
	-- Player Leaving Connection
	Connections.AncenstryCon = plr.AncestryChanged:Connect(function()
		if not plr:IsDescendantOf(game) then
			local char = plr.Character
			if char and char:WaitForChild("Humanoid") then
				char:WaitForChild("Humanoid"):TakeDamage(50 * 50)
			end
			for _, Connection in pairs(Connections) do
				if Connection then
					Connection:Disconnect()
				end
			end
		end
	end)
end)
3 Likes

From what im seeing, you are using :FindFirstChild(), so it will only look one step down. Switching to :GetDecendants() or something will work better

it’s been fixed, its due to the mobile support handler that had a variable failing from time to time.