Custom inventory script breaks tools

I am trying to have a custom inventory hotbar for my game but it breaks the ingame tools I know this because when I disable the script and re-enable the roblox default hotbar tools work perfectly

Script:

game:GetService('StarterGui'):SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
local uis = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local char = workspace:WaitForChild(player.Name) 
local bp = player.Backpack
local hum: Humanoid = char:WaitForChild("Humanoid")
local frame = script.Parent.Slot
local equipped = 0.7
local unequipped = 0
frame.Visible = true

local inputKeys = { 
	["One"] = {txt = "1"},
	["Two"] = {txt = "2"},
	["Three"] = {txt = "3"},
	["Four"] = {txt = "4"},
	["Five"] = {txt = "5"},
}

local inputOrder = { 
	inputKeys["One"],inputKeys["Two"],inputKeys["Three"],inputKeys["Four"],inputKeys["Five"],
}

function handleEquip(tool)
	if tool then
		if tool.Parent ~= char then
			hum:EquipTool(tool)
		else
			hum:UnequipTools()
		end
	end
end

for i, v in pairs(frame:GetChildren()) do
 if v:IsA("ImageLabel") then
v.Tool.MouseButton1Down:Connect(function() 
	for key, value in pairs(inputKeys) do
		if value["txt"] == v.Name then
			handleEquip(value["tool"]) 
		end 
	end
	end)
end	

end

function start() 
	local tools = bp:GetChildren()
	for i = 1, #tools do 
		if tools[i]:IsA("Tool") then 
		for i = 1, #inputOrder do
			local value = inputOrder[i]
			if not value["tool"] then 
				value["tool"] = tools[i]	
				break 
			end
		end
		end
	end
end

function adjust()
	for key, value in pairs(inputKeys) do
		local tool = value["tool"]
		local icon = frame:FindFirstChild(value["txt"])
		if tool then
			icon.Tool.Image = tool.TextureId
			if tool.Parent == char then 
				icon.ImageTransparency = equipped
			else
				icon.ImageTransparency = unequipped
			end
		else
			icon.Tool.Image = ""
			icon.ImageTransparency = unequipped
		end
	end
end

function onKeyPress(inputObject) 
	local key = inputObject.KeyCode.Name
	local value = inputKeys[key]
	if value and uis:GetFocusedTextBox() == nil then 
		handleEquip(value["tool"])
	end 
end

function handleAddition(adding)
	if adding:IsA("Tool") then
		local new = true
		for key, value in pairs(inputKeys) do
			local tool = value["tool"]
			if tool then
				if tool == adding then
					new = false
				end
			end
		end
		if new then
			for i = 1, #inputOrder do
				local tool = inputOrder[i]["tool"]
				if not tool then 
					inputOrder[i]["tool"] = adding
					break
				end
			end
		end
	adjust()
	end
end

function handleRemoval(removing) 
	if removing:IsA("Tool") then
		if removing.Parent ~= char and removing.Parent ~= bp then
			for i = 1, #inputOrder do
				if inputOrder[i]["tool"] == removing then
					inputOrder[i]["tool"] = nil
					break
				end
			end
		end
		adjust()
	end
end

uis.InputBegan:Connect(onKeyPress)
char.ChildAdded:Connect(handleAddition)
char.ChildRemoved:Connect(handleRemoval)
bp.ChildAdded:Connect(handleAddition)
bp.ChildRemoved:Connect(handleRemoval)
start()

Nevermind I found a fix to the problem

Its funny how you need the soloution and you find it yourself 10 minutes later

3 Likes

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