I have a script that takes parts from a model named pack and creates buttons that each represent an item in a backpack. When working properly, if the button is clicked you can place the part that it represents on the ground. For some reason, the main function that makes the player able to place the item on the ground isn't working. I know the other functions (most likely the update() function) is messing it up because I tried deleting all but this one function and it worked by itself. My question is, how could other functions make a onMouseButton1Down() function not work? let me know if I need to clarify anything. thanks!
local BUTTON = script.Parent
local player = game.Players.LocalPlayer
local MFRAME = game.Players.LocalPlayer.PlayerGui.BackpackGui.MFrame
local IFRAME = game.Players.LocalPlayer.PlayerGui.BackpackGui.MFrame.IFrame
local OPEN = game.Players.LocalPlayer.PlayerGui.BackpackGui.Backpack:FindFirstChild("Open")
local mouse=player:GetMouse()
local RANGE = 15
local BP_SIZE = 20
------------------------------
wait(3)
local CURRENT = nil
MFRAME.Visible = false
-------------------------------
function backpackCheck()
local pack = player.Pack
local number_stuff = #pack:GetChildren()
return number_stuff < BP_SIZE
end
function removeExample()
local player = game.Players.LocalPlayer
local char = player.Character
local ex_check = char:findFirstChild("BACKPACK_EXAMPLE")
if ex_check ~= nil then
ex_check:remove()
end
end
function rangeCheck(mouse)
local player = game.Players.LocalPlayer
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 onGuiClick(part)
CURRENT = part
update()
end
function update()
local player = game.Players.LocalPlayer
local char = player.Character
local pack = player:findFirstChild("Pack")
local pack2 = player:findFirstChild("Pack2")
local pack_tab = pack:GetChildren()
local Ipack_tab = IFRAME:GetChildren()
if CURRENT == nil then print ("current is nil")
local x = 0
local y = 0
for index, child in pairs(pack_tab) do
local CHOICE_GUI = MFRAME.TextButton:clone()
CHOICE_GUI.Size = UDim2.new(0, 100, 0, 20)
CHOICE_GUI.Transparency = .4
CHOICE_GUI.BackgroundColor3 = Color3.new()
local new_gui = CHOICE_GUI
new_gui.Position = UDim2.new(0, x, 0, y)
new_gui.Text = child.Name
new_gui.TextColor3 = Color3.new(255,255,255)
new_gui.Name = child.Name
new_gui.MouseButton1Down:connect(function() onGuiClick(child) end)
new_gui.Parent = IFRAME
new_gui.Visible = true
local CHILD = pack:FindFirstChild(child.Name)
CHILD.Parent = pack2
y = y + 20
if y > 240 then
y = 0
x = x + 100
end
end
--cleanGui()
end
end
function do_remove_all(parent)
local children = parent:GetChildren()
for index, child in pairs(children) do
child:remove()
end
end
function rangeCheck(mouse)
local char = player.Character
local torso = char.Torso
return (torso.Position - mouse.hit.p).magnitude < RANGE
end
function removeExample()
local player = game.Players.LocalPlayer
local char = player.Character
local ex_check = char:findFirstChild("BACKPACK_EXAMPLE")
if ex_check ~= nil then
ex_check:remove()
end
end
function onMouseMove(mouse)
local player = game.Players.LocalPlayer
local char = player.Character
local torso = char.Torso
local pack2 = player:findFirstChild("Pack2")
local pack_tab = pack2:GetChildren()
local partname = BUTTON.Text
local part = pack2:FindFirstChild(partname)
local CURRENT = part
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)
local pack2 = player:findFirstChild("Pack2")
local pack_tab = pack2:GetChildren()
local partname = BUTTON.Text
local part = pack2:FindFirstChild(partname)
local player = game.Players.LocalPlayer
local char = player.Character
local example = char:FindFirstChild("example")
local CURRENT = part
--if CURRENT ~= nil then
if rangeCheck(mouse) then
removeExample()
print("clicked")
CURRENT.Parent = game.Workspace
CURRENT.Locked = false
CURRENT.Anchored = false
CURRENT.Position = mouse.hit.p
CURRENT = nil
--do_remove_all(example)
--cleanGui()
script.Parent.Visible = false
script.Parent:Destroy() print("buttonremoved")
update() print("primaryupdate")
end
end
--end
function onKeyDown(key, mouse)
if key == "r" then
removeExample()
local CURRENT = nil
elseif key == "e" then
local player = game.Players.LocalPlayer
local pack = player:findFirstChild("Pack")
local pack_tab = pack:GetChildren()
for index, child in pairs(pack_tab) do
local CURRENT = child
onButton1Down(mouse)
wait(0.1)
end
local CURRENT = nil
end
end
while wait(3) do
update()
print("yesss")
end
function onMouseButton1Down()
print("buttonclicked")
mouse.Button1Down:connect(function() onButton1Down(mouse) end)
mouse.Move:connect(function() onMouseMove(mouse) end)
mouse.KeyDown:connect(function(key) onKeyDown(key, mouse) end)
end
BUTTON.MouseButton1Down:connect(onMouseButton1Down)
