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

How can I turn a tool script into a button script?

So I am trying to make a script that is activated by a tool (being equipped) work the same way but when a button is clicked instead. I tried just replacing the onSelected() with onMouseButton1Down() and removing the onDeSelected() but now it does what its supposed to before I click the button. (when i click with the mouse anywhere). My question is how can I get it to work ONLY when I click the button rather than when I click a part? here it is

wait(3)

local BUTTON = script.Parent

local SCREEN_GUI = BUTTON.ScreenGui
local FRAME = SCREEN_GUI.Frame

local TARG_LABEL = FRAME.NameLabel
local PROG_LABEL = FRAME.ProgressLabel

local PROG_FRAME = FRAME.ProgressFrame
local PROG_BAR = PROG_FRAME.ProgressBar

local FORAGING = false
local DEBOUNCE = true

local RANGE = 15
local TIME_DIV = 25
local BP_SIZE = 10

function getPC()
	local player = game.Players.LocalPlayer
	local char = player.Character

	return player, char
end

function getTime(part)
	return part:GetMass() / TIME_DIV
end

function getForage(part)
	local forage = part.Parent:findFirstChild("Forage")

	return forage
end

function lockedCheck(part)
	return not part.Locked
end

function forageCheck(part)
	local f_check = part.Parent:findFirstChild("Forage")

	return f_check ~= nil
end

function backpackCheck()
	local player, char = getPC()
	local pack = player.Pack
	local number_stuff = #pack:GetChildren()

	return number_stuff < BP_SIZE
end

function distCheck(part)
	local player, char = getPC()
	local head = char.Head

	local dist = (part.Position - head.Position).magnitude

	return dist <= RANGE
end

function not_in_rainbow(part)
	local parent = part.Parent

	if parent.Name == "RainbowStones" then
		return false
	else
		return true
	end
end

function debounceCheck()
	return not FORAGING
end

function canUse(part)
	if part == nil then
		return false
	end

	local bool = false

	bool = (bool or lockedCheck(part))
	bool = (bool or forageCheck(part))
	bool = (bool and distCheck(part))
	bool = (bool and not_in_rainbow(part))

	return bool
end

function updateProgress(time, part)
	PROG_BAR.Size = UDim2.new(time, 0, 1, 0)

	PROG_LABEL.Visible = true
	PROG_LABEL.Text = "<<< "..part.Name..": "..tostring(time * 100).."%"
end

function finishProgress()
	PROG_BAR.Size = UDim2.new(0, 0, 1, 0)

	PROG_LABEL.Visible = false
	PROG_LABEL.Text = "<<<"
end

function forageTarget(part)
	if DEBOUNCE then
		local sBool = false

		if type(part) == "string" then
			sBool = true

			local special = game.Lighting.IF:findFirstChild(part)

			if special ~= nil then
				part = special
			else
				return
				print("DUZ NAWT EXIST j00 BUM!")
			end
		end

		DEBOUNCE = false

		local player, char = getPC()
		local pack = player.Pack
		local human = char.Humanoid

		human.WalkSpeed = 0

		local time = math.floor(getTime(part))

		time = math.max(time, 0.1)

		if sBool then
			time = time * 5
		end

		for t = 0, time, 0.1 do
			updateProgress(t / time, part)

			wait(0.1)

			if not FORAGING then
				break
			end
		end

		wait(0.5)

		finishProgress()

		human.WalkSpeed = 16

		if FORAGING then
			if sBool then
				part:clone().Parent = pack
			else
				part.Parent = pack
			end
		end

		DEBOUNCE = true

	end
end

function onButton1Down(mouse)
	local targ = mouse.Target

	if canUse(targ) and debounceCheck() then
		FORAGING = true

		if targ.Locked then
			local forage = getForage(targ)

			if forage.Value == "" then
				forageTarget(targ)
			else
				forageTarget(forage.Value)
			end
		else
			forageTarget(targ)
		end

		FORAGING = false
	end
end

function onMouseMove(mouse)
	local targ = mouse.Target

	TARG_LABEL.Text = ""

	if canUse(targ) then
		if targ.Locked then
			local forage = getForage(targ)

			if forage.Value == "" then
				TARG_LABEL.Text = targ.Name
			else
				TARG_LABEL.Text = forage.Value
			end
		else
			TARG_LABEL.Text = targ.Name
		end
	end
end

function onMouseButton1Down(mouse)
	local player = game.Players.Localplayer
	local p_gui = player.PlayerGui
	local Bpack = player.Backpack

	SCREEN_GUI.Parent = p_gui
	

	mouse.Button1Down:connect(function()
		onButton1Down(mouse)
	end)

	mouse.Move:connect(function()
		onMouseMove(mouse)
	end)

	mouse.KeyDown:connect(function(key)
		if key == "x" then
			FORAGING = false
			PROG_LABEL.Text = "CANCELLED"
		end
	end)
end


BUTTON.MouseButton1Down:connect(onMouseButton1Down)

1 answer

I think the problem is that the Button is a Parent of the ScreenGui.

The ScreenGui is supposed to be the parent of the Button.

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