I've been trying to change this backpack script from opening/creating the backpack when the tool is selected to opening/creating when a GUI button is clicked. I've tried changing the function from onSelected() to onMouseButton1Down() but it doesn't seem to be working. What am I doing wrong/ missing? Thanks guys!

wait(3)

local TOOL = script.Parent
local BUTTON = script.Parent
local GUI = BUTTON.Parent.Parent

local CHOICE_GUI = Instance.new("TextButton")
CHOICE_GUI.Name = "BACKPACK_CHOICE"
CHOICE_GUI.Size = UDim2.new(0, 100, 0, 25)

local CURRENT = nil

local RANGE = 15

function removeExample()
	local player = TOOL.Parent.Parent
	local char = player.Character

	local ex_check = char:findFirstChild("BACKPACK_EXAMPLE")

	if ex_check ~= nil then
		ex_check:remove()
	end
end

function cleanGui()
	local player = TOOL.Parent.Parent
	local playerGui = player.PlayerGui
	local tab = playerGui.ScreenGui:GetChildren()

	for index, child in pairs(tab) do
		if child.Name == "BACKPACK_CHOICE" then
			child:remove()
		end
	end
end

function rangeCheck(mouse)
	local player = TOOL.Parent.Parent
	local char = player.Character
	local torso = char.Torso

	return (torso.Position - mouse.hit.p).magnitude < RANGE
end

function do_remove_all(parent)
	local children = parent:GetChildren()

	for index, child in pairs(children) do
		child:remove()
	end
end

function onMouseMove(mouse)
	local player = TOOL.Parent.Parent
	local char = player.Character
	local torso = char.Torso

	removeExample()

	if CURRENT ~= nil then
		if rangeCheck(mouse) then
			local example = CURRENT:clone()
			example.Name = "BACKPACK_EXAMPLE"
			example.Locked = true
			example.Transparency = 0.5
			example.Parent = char
			example.Position = mouse.hit.p
			do_remove_all(example)
		end
	end
end

function onButton1Down(mouse)
	if CURRENT ~= nil then
		if rangeCheck(mouse) then
			removeExample()

			CURRENT.Parent = game.Workspace
			CURRENT.Locked = false
			CURRENT.Anchored = false
			CURRENT.Position = mouse.hit.p
			CURRENT = nil
		end
	end

	update()
end

function onKeyDown(key, mouse)
	if key == "r" then
		removeExample()
		CURRENT = nil
	elseif key == "e" then
		local player = TOOL.Parent.Parent
		local pack = player:findFirstChild("Pack")
		local pack_tab = pack:GetChildren()

		for index, child in pairs(pack_tab) do
			CURRENT = child
			onButton1Down(mouse)
			wait(0.1)
		end

		CURRENT = nil
	end

	update()
end

function onGuiClick(part)
	CURRENT = part
	update()
end

function update()
	local player = GUI.Parent.Parent
	local pack = player:findFirstChild("Pack")
	local pack_tab = pack:GetChildren()

	if CURRENT == nil then
		local x = 0
		local y = 250

		for index, child in pairs(pack_tab) do
			local new_gui = CHOICE_GUI:clone()
			new_gui.Position = UDim2.new(0, x, 0, y)
			new_gui.Text = child.Name
			new_gui.MouseButton1Down:connect(function() onGuiClick(child) end)
			new_gui.Parent = player.PlayerGui.ScreenGui

			y = y + 25

			if y > 350 then
				y = 250
				x = x + 100
			end
		end
	else
		cleanGui()
	end
end
---------------- New function below
function onMouseButton1Down()
BUTTON.MouseButton1Down:connect(function() onMouseButton1Down(BUTTON) end)
	BUTTON.Move:connect(function() onMouseMove(BUTTON) end)
	BUTTON.KeyDown:connect(function(key) onKeyDown(key, BUTTON) end)
	

	update()
end
------------------------------------------------------------------
function onSelected()
	mouse.Button1Down:connect(function() onButton1Down(mouse) end)
	mouse.Move:connect(function() onMouseMove(mouse) end)
	mouse.KeyDown:connect(function(key) onKeyDown(key, mouse) end)

	update()
end
------- Old Function for Selecting tool^
-------Old Function
--function onDeselected()
--	CURRENT = nil
--	removeExample()
--	cleanGui()
--end
-------Old Function for deselecting^
------------------------------------------------------------------------------------
BUTTON.MouseButton1Down:connect(onMouseButton1Down)
--TOOL.Deselected:connect(onDeselected)
--TOOL.Selected:connect(onSelected)

2 comments

1

Any errors, what is it actually doing. You haven't been very specific...

0

Basically this is a backpack script where you click on it and gui button pop up that represent the items in your backpack. Originally this script was in a tool so when you click on the tool the backpack opens, but I'm trying to make the script work in a GUI button instead of a tool