Q is being printed multiple times?

Heyo! I’m currently making a shooter, and I have this ability called the floating arrow, as of right now it just follows the player and destroys stuff when you press Q, problem is it seems Q is firing multiple times and i feel as that is causing lag, any idea and how to stop this and stop the lag aswell?

code
local TweenService = game:GetService("TweenService")
local RunService = game:GetService("RunService")

repeat wait() until game.Players.LocalPlayer
local debounce3 = false
local debounce2 = false
local debounce = 0
local player = script.Parent.Parent
local mouse = player:GetMouse()
local spear = workspace.Spear
local LocalPlayer = game:GetService("Players").LocalPlayer
local Character = LocalPlayer.Character
local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
local folder = workspace.Egg
local hum = Character:FindFirstChild("Humanoid")
local anim = hum:LoadAnimation(script:FindFirstChildOfClass("Animation"))
local tweenInfo = TweenInfo.new(
	0.5, -- Time
	Enum.EasingStyle.Linear, -- EasingStyle
	Enum.EasingDirection.Out, -- EasingDirection
	0, -- RepeatCount (when less than zero the tween will loop indefinitely)
	false, -- Reverses (tween will reverse once reaching it's goal)
	0 -- DelayTime
)
local tween = TweenService:Create(spear.pet, tweenInfo, {Orientation = Vector3.new(90, 0, 0)})

mouse.KeyDown:connect(function(key)
	if key == "e" and debounce <=0 and debounce2 == false
	then
		debounce += 1
		local clone = spear:Clone()
		clone.Parent = folder
		clone.Name = "spear" .. debounce
		print("e pressed")
		local HRPOS = HumanoidRootPart.CFrame
		local HRPOR = HumanoidRootPart.Orientation
		clone.pet.CFrame = HRPOS
		clone.pet.Orientation = HRPOR
		local x2 = HRPOR.X
		local Y2 = HRPOR.Y
		local tween = TweenService:Create(clone.pet, tweenInfo, {Orientation = Vector3.new(90, Y2, x2)})
	end
	RunService.RenderStepped:Connect(function(step)
		
		local HRPOS = HumanoidRootPart.CFrame
		local HRPOR = HumanoidRootPart.Orientation
		local x2 = HRPOS.X
		local Y2 = HRPOS.Y
		local Z = HRPOS.Z
		if debounce == 1 then
			local tween2 = TweenService:Create(folder.spear1.pet, tweenInfo, {Position = Vector3.new(x2 + 3, Y2 + 2, Z)})
			local x4 = HRPOR.X
			local Y3 = HRPOR.Y
			local tween = TweenService:Create(folder.spear1.pet, tweenInfo, {Orientation = Vector3.new(90, Y3, x4)})
			tween:Play()
			tween2:Play()
			wait(0.5)
		elseif debounce == 2 then
			local x4 = HRPOR.X
			local Y3 = HRPOR.Y
			local tween2 = TweenService:Create(folder.spear1.pet, tweenInfo, {Position = Vector3.new(x2 + 3, Y2 + 2, Z)})
			local tween = TweenService:Create(folder.spear1.pet, tweenInfo, {Orientation = Vector3.new(90, Y3, x4)})
			tween:Play()
			tween2:Play()
			local x4 = HRPOR.X
			local Y3 = HRPOR.Y
			local tween4 = TweenService:Create(folder.spear2.pet, tweenInfo, {Position = Vector3.new(x2, Y2 + 3, Z)})
			local tween3 = TweenService:Create(folder.spear2.pet, tweenInfo, {Orientation = Vector3.new(90, Y3, x4)})
			tween4:Play()
			tween3:Play()
			wait(0.5)
		elseif debounce == 3 then
			local tween2 = TweenService:Create(folder.spear1.pet, tweenInfo, {Position = Vector3.new(x2 + 3, Y2 + 2, Z)})
			local x4 = HRPOR.X
			local Y3 = HRPOR.Y
			local tween = TweenService:Create(folder.spear1.pet, tweenInfo, {Orientation = Vector3.new(90, Y3, x4)})
			local tween4 = TweenService:Create(folder.spear2.pet, tweenInfo, {Position = Vector3.new(x2, Y2 + 3, Z)})
			local tween3 = TweenService:Create(folder.spear2.pet, tweenInfo, {Orientation = Vector3.new(90, Y3, x4)})
			local tween5 = TweenService:Create(folder.spear3.pet, tweenInfo, {Position = Vector3.new(x2 + -3 , Y2 + 2, Z)})
			local tween6 = TweenService:Create(folder.spear3.pet, tweenInfo, {Orientation = Vector3.new(90, Y3, x4)})
			tween:Play()
			tween2:Play()
			tween4:Play()
			tween3:Play()
			tween5:Play()
			tween6:Play()
			wait(0.5)
			
			
		end
	end)
	local uis = game:GetService("UserInputService")
	local Character = LocalPlayer.Character
	uis.InputBegan:Connect(function(input)
		if input.KeyCode == Enum.KeyCode.Q and debounce == 1 and folder.spear1 and debounce3 == false then
			debounce2 = true
			print("qpress")
			local rayOrigin = HumanoidRootPart.Position
			local rayDirection = (rayOrigin + HumanoidRootPart.CFrame.LookVector) * 50;
			local raycastParams = RaycastParams.new()
			raycastParams.FilterDescendantsInstances = {HumanoidRootPart.Parent,folder}
			raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
			local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)
			local ray = Ray.new(rayOrigin,rayDirection)
			if raycastResult then
				local object = raycastResult.Instance
				print("rayfound!")
				if object and folder.spear1 then
					print("raycasted")
					anim:Play()
				debounce -= 1
				folder.spear1.Parent = folder
				local tweenInfo2 = TweenInfo.new(
					0.5, -- Time
					Enum.EasingStyle.Linear, -- EasingStyle
					Enum.EasingDirection.Out, -- EasingDirection
					0, -- RepeatCount (when less than zero the tween will loop indefinitely)
					false, -- Reverses (tween will reverse once reaching it's goal)
					0 -- DelayTime
					)
					local lookpart = folder.spear1.pet
					local unit = -(folder.spear1.pet.CFrame.p - object.Position).unit
					local tween10 = TweenService:Create(folder.spear1.pet, tweenInfo2, {Position = object.Position + Vector3.new(-3,0,0)})
					local lookAt = folder.spear1.pet.CFrame:PointToWorldSpace(Vector3.new(0,0,-3)):Lerp(object.Position,.1)
					folder.spear1.pet.CFrame = CFrame.lookAt(folder.spear1.pet.Position, lookAt, folder.spear1.pet.CFrame.UpVector)
					tween10:Play()
					wait(0.6)
					object:Destroy()
					tween10:Cancel()
					anim:Stop()
					debounce += 1
					wait(1)
					debounce2 = false
					end
			end
		end
	end)
end)


Pardon my horrendous code, I’m a beginner.

Your script is checking if debouce3 is false, but sets debouce2 to true instead

Sorry I forgot to remove debounce3! If i were to add a debounce it wouldnt run again if there was no outcome

You’re creating event connections inside of other event connections (nested connections), event connections stack not override. You can resolve this by creating event connections once (preferably when the script is initially ran).

1 Like

You’re not disconnecting the functions so it runs multiple times.

New code:

--//Services
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")

--//Variables
local LocalPlayer = Players.LocalPlayer
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
local hum = Character:FindFirstChild("Humanoid")
local anim = hum:LoadAnimation(script:FindFirstChildOfClass("Animation"))

local mouse = LocalPlayer:GetMouse()
local spear = workspace.Spear
local folder = workspace.Egg

--//Controls
local debounce3 = false
local debounce2 = false
local debounce = 0

local tweenInfo = TweenInfo.new(
	0.5, -- Time
	Enum.EasingStyle.Linear, -- EasingStyle
	Enum.EasingDirection.Out, -- EasingDirection
	0, -- RepeatCount (when less than zero the tween will loop indefinitely)
	false, -- Reverses (tween will reverse once reaching it's goal)
	0 -- DelayTime
)

local tweenInfo2 = TweenInfo.new(
	0.5, -- Time
	Enum.EasingStyle.Linear, -- EasingStyle
	Enum.EasingDirection.Out, -- EasingDirection
	0, -- RepeatCount (when less than zero the tween will loop indefinitely)
	false, -- Reverses (tween will reverse once reaching it's goal)
	0 -- DelayTime
)

local tween = TweenService:Create(spear.pet, tweenInfo, {Orientation = Vector3.new(90, 0, 0)})

--//Tables
local Connections = {}
local DebounceFunctions = {}

--//Functions
DebounceFunctions[1] = function(x2, Y2, Z, x4, Y3)
	local tween = TweenService:Create(folder.spear1.pet, tweenInfo, {Orientation = Vector3.new(90, Y3, x4)})
	tween:Play()

	local tween2 = TweenService:Create(folder.spear1.pet, tweenInfo, {Position = Vector3.new(x2 + 3, Y2 + 2, Z)})
	tween2:Play()

	task.wait(0.5)
end

DebounceFunctions[2] = function(x2, Y2, Z, x4, Y3)
	local tween = TweenService:Create(folder.spear1.pet, tweenInfo, {Orientation = Vector3.new(90, Y3, x4)})
	tween:Play()

	local tween2 = TweenService:Create(folder.spear1.pet, tweenInfo, {Position = Vector3.new(x2 + 3, Y2 + 2, Z)})
	tween2:Play()

	local tween3 = TweenService:Create(folder.spear2.pet, tweenInfo, {Orientation = Vector3.new(90, Y3, x4)})
	tween3:Play()

	local tween4 = TweenService:Create(folder.spear2.pet, tweenInfo, {Position = Vector3.new(x2, Y2 + 3, Z)})
	tween4:Play()

	task.wait(0.5)
end

DebounceFunctions[3] = function(x2, Y2, Z, x4, Y3)
	local tween = TweenService:Create(folder.spear1.pet, tweenInfo, {Orientation = Vector3.new(90, Y3, x4)})
	tween:Play()

	local tween2 = TweenService:Create(folder.spear1.pet, tweenInfo, {Position = Vector3.new(x2 + 3, Y2 + 2, Z)})
	tween2:Play()

	local tween3 = TweenService:Create(folder.spear2.pet, tweenInfo, {Orientation = Vector3.new(90, Y3, x4)})
	tween3:Play()

	local tween4 = TweenService:Create(folder.spear2.pet, tweenInfo, {Position = Vector3.new(x2, Y2 + 3, Z)})
	tween4:Play()

	local tween5 = TweenService:Create(folder.spear3.pet, tweenInfo, {Position = Vector3.new(x2 + -3 , Y2 + 2, Z)})
	tween5:Play()

	local tween6 = TweenService:Create(folder.spear3.pet, tweenInfo, {Orientation = Vector3.new(90, Y3, x4)})
	tween6:Play()

	task.wait(0.5)
end

UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if gameProcessedEvent then
		return
	end
	
	for i, connection in pairs(Connections) do
		connection:Disconnect()
		Connections[i] = nil
	end
	
	if input.KeyCode == Enum.KeyCode.E and debounce <= 0 and not debounce2 then
		print("e pressed")

		debounce += 1
		
		local clone = spear:Clone()
		clone.Name = "spear" .. debounce
		clone.Parent = folder

		local HRPOS = HumanoidRootPart.CFrame
		clone.pet.CFrame = HRPOS

		local HRPOR = HumanoidRootPart.Orientation
		clone.pet.Orientation = HRPOR
		
		local x2 = HRPOR.X
		local Y2 = HRPOR.Y
		
		local tween = TweenService:Create(clone.pet, tweenInfo, {Orientation = Vector3.new(90, Y2, x2)})
	end
	
	Connections.RenderStepped = RunService.RenderStepped:Connect(function(step)
		local HRPOS = HumanoidRootPart.CFrame
		local HRPOR = HumanoidRootPart.Orientation
		
		local x2 = HRPOS.X
		local Y2 = HRPOS.Y
		local Z = HRPOS.Z
		local x4 = HRPOR.X
		local Y3 = HRPOR.Y
		
		DebounceFunctions[debounce](x2, Y2, Z, x4, Y3)
	end)
	
	Connections.InputBegan = UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
		if gameProcessedEvent or input.KeyCode ~= Enum.KeyCode.Q or debounce ~= 1 or not debounce3 then
			return
		end
		
		print("qpress")
		
		debounce3 = true

		local rayOrigin = HumanoidRootPart.Position
		local rayDirection = (rayOrigin + HumanoidRootPart.CFrame.LookVector) * 50;

		local raycastParams = RaycastParams.new()
		raycastParams.FilterDescendantsInstances = {HumanoidRootPart.Parent, folder}
		raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

		local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)
		
		if not raycastResult then
			return
		end
		
		debounce -= 1
		folder.spear1.Parent = folder
		
		local object = raycastResult.Instance
		anim:Play()

		local lookpart = folder.spear1.pet
		local unit = -(folder.spear1.pet.CFrame.Position - object.Position).Unit
		
		local tween10 = TweenService:Create(folder.spear1.pet, tweenInfo2, {Position = object.Position + Vector3.new(-3,0,0)})
		tween10:Play()

		local lookAt = folder.spear1.pet.CFrame:PointToWorldSpace(Vector3.new(0,0,-3)):Lerp(object.Position,.1)
		folder.spear1.pet.CFrame = CFrame.lookAt(folder.spear1.pet.Position, lookAt, folder.spear1.pet.CFrame.UpVector)
		
		task.wait(0.6)
		object:Destroy()
		
		tween10:Cancel()
		anim:Stop()
		debounce += 1
		
		task.wait(1)
		debounce3 = false
	end)
end)
1 Like

Oh wow! Thanks for rewriting the code for me, You really didn’t have to, but I appreciate it! I’ll try to clean up my code next time.

1 Like

No problem. If you have any more questions, feel free to ask.

1 Like