How do i fix my inventory script

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?
    Im trying to make my inventory not make each item Slot into the same item but if I add a break it will only make the script run once
  2. **What is the issue?**S
    Stated Above
  3. What solutions have you tried so far?
    I’ve tried adding a break but that just breaks the script
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local WeaponMenu = script.Parent
local Weapons = char:FindFirstChild("WeaponInventory")
local WeaponEquipEvent = game:GetService("ReplicatedStorage").Combat.WeaponEquip

if Weapons then
	Weapons.ChildAdded:Connect(function()
		print("UPDATING WEAPONS")
		for _, WeaponsInv in pairs(Weapons:GetDescendants()) do
			if WeaponsInv:IsA("BoolValue") or WeaponsInv:IsA("NumberValue") or WeaponsInv:IsA("IntValue")  then
				for _, WeaponSlot in pairs(script.Parent:GetDescendants()) do
					if WeaponSlot:IsA("Frame") then
						WeaponSlot:FindFirstChild("TextLabel").Text = WeaponsInv.Name
						WeaponSlot.Name = WeaponsInv.Name
						if WeaponSlot.Name == WeaponsInv.Name then
							print("Stopping Loop")
							local WeaponsButton =	WeaponSlot:FindFirstChild("TextButton")
							if WeaponsButton then
								WeaponsButton.MouseButton1Click:Connect(function()
									if not plr:GetAttribute("WeaponEquipped") then
										WeaponSlot:FindFirstChild("TextLabel").TextColor3 = Color3.fromRGB(0, 255, 0)
										WeaponEquipEvent:FireServer(WeaponSlot, WeaponsInv)
									end
								end)
							end
						end
					end
				end
			end
		end
	end)
end


Try using continue instead

(Also consider using some guard clauses so you don’t have 10 tabs due to if statements)

I really don’t know what Guard Clauses are sorry im fairly new

I tried and I got the same result but maybe im placing it incorrectly

The idea for guard clauses are relatively simple. I’ll use your code as an example

-- This uses a conventional if statement
if Weapons then
    dostuff()
end

-- This is a guard clause, instead we check the reverse, and if the reverse is true you 
-- return or use continue if you're in a loop
if not Weapons then return end
dostuff()

So your code would become:

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local WeaponMenu = script.Parent
local Weapons = char:FindFirstChild("WeaponInventory")
local WeaponEquipEvent = game:GetService("ReplicatedStorage").Combat.WeaponEquip

if not Weapons then return end
Weapons.ChildAdded:Connect(function()
	print("UPDATING WEAPONS")
	for _, WeaponsInv in pairs(Weapons:GetDescendants()) do
		if not WeaponsInv:IsA('ValueBase') then continue end
		for _, WeaponSlot in pairs(script.Parent:GetDescendants()) do
			if not WeaponSlot:IsA('Frame') then continue end
			WeaponSlot:FindFirstChild("TextLabel").Text = WeaponsInv.Name
			WeaponSlot.Name = WeaponsInv.Name
			if WeaponSlot.Name ~= WeaponsInv.Name then continue end
			print("Stopping Loop")
			local WeaponsButton = WeaponSlot:FindFirstChild("TextButton")
			if not WeaponsButton then continue end
			WeaponsButton.MouseButton1Click:Connect(function()
				if plr:GetAttribute("WeaponEquipped") then return end
				WeaponSlot:FindFirstChild("TextLabel").TextColor3 = Color3.fromRGB(0, 255, 0)
				WeaponEquipEvent:FireServer(WeaponSlot, WeaponsInv)
			end)
		end
	end
end)

Granted readability isn’t the greatest due to other factors, but that’s general idea of guard clauses and it does definitely help

1 Like

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