I am trying to change this script from happening when you click on the tool to happening when a GUI button is clicked. I may be wrong but I don't think onSelected() and onDeselected() work on GUI buttons, so I've tried substituting onButton1Down() and onMouseButton1Down() for those functions but they don't seem to work. Any suggestions for what function I should use to make successfully do this? Or is it even possible without restructuring the whole script?
wait(3)
local TOOL = script.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 = TOOL.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
function onSelected(mouse)
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
function onDeselected()
CURRENT = nil
removeExample()
cleanGui()
end
TOOL.Selected:connect(onSelected)
TOOL.Deselected:connect(onDeselected)

1 comment
I know from GUIs, that MouseButton1Click and Button1Down don't have on's in front of them. ;)