Help on scripting a tool

Hi, so I am trying to script a tool that if you hold your mouse while having the tool equipped for around 2 seconds then they can dash forward otherwise they will just use the sword normally.

I got the dashing part done but the problem is that sometimes the player dashes when they don’t hold it for the duration of time or they dash when the bar doesn’t hit max.

Local script:

local Sword = script.Parent

local UserInputService = game:GetService("UserInputService")

local SwordClient = require(game.ReplicatedStorage:WaitForChild("ClientModules"):WaitForChild("Sword"):WaitForChild("SwordGui"))

local player = game.Players.LocalPlayer
local playerGui = player.PlayerGui
local character = player.Character or player.CharacterAdded:Wait()
local Humanoid = character:WaitForChild("Humanoid")

local SwordUI = playerGui:WaitForChild("SwordUI")

local EquipAnim = Humanoid:LoadAnimation(Sword.Equip)
local JSlash = Humanoid:LoadAnimation(Sword.JSlash)
local Slash = Humanoid:LoadAnimation(Sword.Slash)

local Use = false
local IsHolding = false
local Dash = false	
local Debounce = false
local Stop = Sword.Stop

Sword.Equipped:Connect(function()
	EquipAnim:Play()
	
	EquipAnim.Stopped:Connect(function()
		Use = true
	end)
end)

Sword.Unequipped:Connect(function()
	Use = false
	Dash = false
	IsHolding = false
	Stop.Value = true
end)

UserInputService.InputBegan:Connect(function(input)
	if Sword.Parent == character then
		if input.UserInputType == Enum.UserInputType.MouseButton1 and Debounce == false and Use == true then
			IsHolding = true
			Dash = false
			
			task.wait(.75)
			
			if UserInputService:IsMouseButtonPressed(Enum.UserInputType.MouseButton1) then
				if IsHolding == true and Sword.Parent == character then
					SwordClient:CDTween(player,1,Sword)
				end
			end

			task.wait(1)

			if UserInputService:IsMouseButtonPressed(Enum.UserInputType.MouseButton1) then
				if IsHolding == true and Sword.Parent == character and SwordUI:FindFirstChild("ClonedGui") then
					Dash = true
				end
			end
		end
	end
end)

UserInputService.InputEnded:Connect(function(input)
	if Sword.Parent == character then
		IsHolding = false
		
		if input.UserInputType == Enum.UserInputType.MouseButton1 and Debounce == false and Use == true then
			Debounce = true
			Stop.Value = true
			
			if SwordUI:FindFirstChild("ClonedGui") then
				SwordUI:FindFirstChild("ClonedGui"):Destroy()
			end

			if Dash == true then
				JSlash:Play()
				Sword.Fire:FireServer("Dash")
				Dash = false
				task.wait(2.5)			
				Debounce = false
			elseif Dash == false then
				Slash:Play()
				Sword.Fire:FireServer("Normal")
				task.wait(.25)
				Debounce = false
			end
			
			Stop.Value = false
		end
	end
end)

Module for gui tween functions

function System:CDTween(player,TweenTime,Sword)
	local BackFrame = player.PlayerGui:WaitForChild("SwordUI"):WaitForChild("BackFrame"):Clone()
	BackFrame.Parent = player.PlayerGui:WaitForChild("SwordUI")
	BackFrame.Name = "ClonedGui"
	
	local TweenFrame = BackFrame:WaitForChild("TweenFrame")

	TweenFrame.BackgroundColor3 = Color3.fromRGB(147, 38, 38)
	BackFrame.Visible = true

	local Tween = TweenService:Create(TweenFrame,TweenInfo.new(TweenTime,Enum.EasingStyle.Quad,Enum.EasingDirection.Out,0,false,0),{Size = UDim2.new(1,0,1,0)})
	Tween:Play()
	
	local EquipConnection
	local UnEquipConnection
	local HeartBeatConnection

	EquipConnection = Sword.Equipped:Connect(function()
		BackFrame.Visible = true
	end)

	UnEquipConnection = Sword.Unequipped:Connect(function()
		BackFrame.Visible = false
	end)
	
	Tween.Completed:Connect(function()
		--BackFrame:Destroy()
		EquipConnection:Disconnect()
		UnEquipConnection:Disconnect()
	end)
	
	--Tween.Completed:Wait()
end

function System:SwordTweenGui(player,Sword,TweenTime)
	local BackFrame = player.PlayerGui:WaitForChild("SwordUI"):WaitForChild("BackFrame"):Clone()
	BackFrame.Parent = player.PlayerGui:WaitForChild("SwordUI")
	local TweenFrame = BackFrame.TweenFrame

	local SwordUnEquipConnection

	TweenFrame.Position = UDim2.new(1,0,0,0)

	BackFrame.Visible = true

	SwordUnEquipConnection = Sword.Unequipped:Connect(function()
		BackFrame.Visible = false
	end)

	local Tween = TweenService:Create(TweenFrame,TweenInfo.new(TweenTime,Enum.EasingStyle.Quad,Enum.EasingDirection.Out,0,false,0),{Size = UDim2.new(-1,0,1,0)})
	Tween:Play()

	Tween.Completed:Connect(function()
		BackFrame.Visible = false
		TweenFrame.Size = UDim2.new(0,0,1,0)
		TweenFrame.Position = UDim2.new(0,0,0,0)
		SwordUnEquipConnection:Disconnect()
	end)
end
2 Likes