Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

What function can I use instead of onSelected() and onDeselected()?

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

0

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

2 answers

I don't have any experience with tools, but I assume it could still work if you add a variable for whether the button is selected or not, and have an extra function which sends it to the Selected/Deselected functions.

wait(3)

local TOOL = script.Parent
local SELECTED = false -- The variable

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)
	if SELECTED then -- May want to add this as well, don't know
		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
end

function onDeselected()
    CURRENT = nil
    removeExample()
    cleanGui()
end

function onClick() -- The function
	if SELECTED then
		SELECTED = false
		onDeselected()
	else
		SELECTED = true
		onSelected()
	end
end

TOOL.Selected:connect(onSelected)
TOOL.Deselected:connect(onDeselected)
nameofthebutton.MouseButton1Down:connect(onClick) -- Connection line (Can also use MouseButton1Click if you want it to fire only when there's been a full click rather than just the mouse being pressed down)

But anyhow, like I said, I've no experience with tools. If it doesn't work I'll try to help more, but hopefully someone with more experience with them will help you out.

2 comments

0

I got the error " attempt to index field 'Selected' (a boolean value)"

0

That's probably because of the connection line with the same name. Try renaming the variable I added to something else (in all the places it's in).

Use "Equipped" and "unequipped" instead.

TOOL.Equipped:connect(function(Random)
--Code
TOOL.Unequipped:connect(function(Random)
--Code

I hope it helped !

This discussion is preserved as it was. New answers, comments, and votes are closed.