Hold to continue activate tool

Hello I am having issues with my Tool Script. Any feedback is very much appreciated!

What do you want to achieve?
Have you ever played Bee Swarm Simulator? How you can click and it swings your tool. But if you hold click it will continuously
use your tool.

What is the issue?
My issue is that sometimes when you click it initiates the loop and continues without end.

Tool Script:

local ReplicatedStorage = game.ReplicatedStorage
local Broadcasts = ReplicatedStorage.Broadcasts
local Modules = ReplicatedStorage.Modules

local Data = require(Modules.Data)
local Item = require(Modules.Item)
local Webhook = require(game.ReplicatedStorage.Modules.Webhook)

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()

local Character = Player.Character
local Humanoid = Character.Humanoid
local Animator = Humanoid.Animator

local ToolDebounce = false
local Breakout = false

local ItemData = Item[script.Parent.Name] 
if not ItemData then
	local NewWebhook = Webhook:CreateWebhook()

	local Embed = Webhook:CreateEmbed()
	Embed:SetColor(Color3.new(1, 0.4, 0.45))
	Embed:SetTitle("TOOL - MISSING DATA")
	Embed:SetDescription(script.Parent.Name.." is missing tool data.")
	Embed:SetFooter(Player.Name.." | "..Player.UserId.." | UTC TICK: "..os.time())

	NewWebhook:AddEmbed(Embed)

	Webhook:SendAsync(NewWebhook, "WEBHOOK")
end


local function Swinging(WaitOnStart)
	task.spawn(function()
		while true do
			if Breakout then return end

			Animator:LoadAnimation(script.Parent.Swing):Play()
			
			print("Swing Tool")
			game.ReplicatedStorage.Broadcasts.Events.SwingTool:FireServer()

			task.wait(ItemData.Rate)
		end
	end)
end

task.spawn(function()
	Mouse.Button1Down:Connect(function()
		Swinging(false)
	end)
end)

task.spawn(function()
	Mouse.Button1Up:Connect(function()
		Breakout = true
		task.wait(ItemData.Rate)
		Breakout = false
	end)
end)

Providing Structure to how to properly do this would be incredibly AMAZING!! :smiley:

You might want to look at [Hold-Click Tools](https://Hold-click tools). This might help you.

Thank you for the referal but this wont work because what if the player only clicks? It wont register.

I’m quite sure this is why sometimes the loop continues without end.

This is solely relying on if they released the button at the same time when the loop restarts.


Imagine a car, driving in a race track, doing exactly one lap every 10 seconds.

You then put down a barrier (at a random time) to stop it from moving.
After 10 seconds, you remove the barrier.

There’s a good chance that the car has already passed after you put it down, and before it could get stopped by it, it got removed. (Since the barrier is taken down after 10 seconds, and car would only be able to come back after 10 seconds)

Basically, if you place the barrier at a bad time, you will never catch the car.
Just like how if you set the Breakout variable to true at the wrong time, and then set it to false at the same speed the loop runs, the loop will never see the variable change, and it will keep going indefinitely.


Anyways sorry for my blabbering, here’s how you fix it:

Remove this from the Button1Up Connection. (Setting it back to false like that is like taking the barrier away before the car could reach it)

You then want to change this condition inside of the swinging loop:

To this:

if Breakout then break end

Calling return will basically stop the entire function, calling break will stop the loop, and allow code that is after the loop, to run.

You can then add a
Breakout = false
after the swing loop, so that it can turn it off the loop without any chance of failing.

So now, your Swinging function should look something like this:

local function Swinging(WaitOnStart)
	task.spawn(function()
		while true do
			if Breakout then break end

			Animator:LoadAnimation(script.Parent.Swing):Play()
			
			print("Swing Tool")
			game.ReplicatedStorage.Broadcasts.Events.SwingTool:FireServer()

			task.wait(ItemData.Rate)
		end

        Breakout = false
	end)
end

Yipee!! Now, instead of that imaginary race car having to be stopped by a possibly desynced barrier, we just added breaks to it so it can stop itself!

So yeah, hopefully my advice works, and hopefully my extremely long response helped you learn something!

1 Like

Thank you! You are amazing! :smiley:

30 whole characters

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.