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

How would I make a function fire after another function has been fired?

I need to know this because I'm trying to make an ax that works by mouse1down and onTouched but I don't know how to do that. Heres the current script (from toolbox)

wait(1)
------------------------
axelvl = 1
treeMoney = 25
resettime = 2
------------------------

Tool = script.Parent
ting = 0 --this is my debounce

function hit()
	print("hitting")
	local anim = Instance.new("StringValue")
	anim.Name = "toolanim"
	anim.Value = "Slash"
	anim.Parent = Tool
end

function onActivated() 
	if not Tool.Enabled then
		return
	end
	
	Tool.Enabled = false
	hit()
	Tool.Enabled = true

end 

function onTouched(hitt)
--	print("touched something")
	if ting == 0 then
	ting = 1
	if hitt.Parent.Name == "Tree" then
--	print("touched tree")
	user = game.Players:findFirstChild(Tool.Parent.Name) --find the player that carries the axe for later moneygiving

	hitt.Parent.hit.Value = hitt.Parent.hit.Value - axelvl --deal damage to the tree 

	if hitt.Parent.hit.Value < 1 and hitt.Parent.Timber.Value == 0 then --check if the tree is felled
		user.leaderstats.Money.Value = user.leaderstats.Money.Value + treeMoney --add money to the dude with the axe
		hitt.Parent.Timber.Value = 1 --this declares the tree "felled" and makes sure nobody else can get money from it
		wait(resettime)
	else
		wait(1)
	end
	end
	ting = 0
	end
end

Tool.Activated:connect(onActivated)
connection = Tool.Handle.Touched:connect(onTouched)

So how would I make function ontouched(hitt) only fire when a mouse1down function is fired?

1 answer

✓ Accepted answer

What you could do is only listen to the event in the Mouse1Down function, and disconnect it after some cooldown. You could do this by creating a connection through :Connect(), and using :Disconnect() on the connection after.

Like so:

function onMouse1Down()
	-- connect to touched event
	local touchConnection = Tool.Handle.Touched:connect(onTouched)

	wait(cooldown)

	-- disconnect
	touchConnection:Disconnect()
end
-- connect your Mouse1Down here

NOTE: the connection to touched shouldn't happen anywhere else in the code, it should only happen in onMouse1Down.

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