Floating arrow optimization

Hey there! Currently making a floating arrow ability for my game!
As of right now its very buggy and laggy, i’d like to try to optimize it, I’m a fairly new scripter and started around 2 weeks ago, so my code is very bad, it seems as when you press Q to launch the arrow it fires it multiple times. please give me feedback on my horrible code! thanks!

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)
			local Distance = (raycastResult.Position - folder.spear1.pet.Position).magnitude
			local Direction = (raycastResult.Position - folder.spear1.pet.Position)
			local Laser = Instance.new("Part",workspace)
			local MiddlePoint = folder.spear1.pet.Position + Direction/2
			Laser.Name = "Laser"
			Laser.CFrame = CFrame.new(MiddlePoint,raycastResult.Position) -- We position the Laser at the middle point, facing the raycast position
			-- The size is gonna be equal to the distance on the Z axis.
			Laser.Size = Vector3.new(1,1,Distance)
			Laser.Material = Enum.Material.Neon
			Laser.Anchored = true
			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)})
					
					tween10:Play()
					wait(0.6)
					object:Destroy()
					tween10:Cancel()
					anim:Stop()
					debounce += 1
					wait(1)
					debounce2 = false
					end
			end
		end
	end)
end)



2 Likes

Try using UserInputService instead

Keyboard Input Part.
I’ll try to improve the arrow’s movement

I have made a simple align position constraint based spear (no scripts used)
spear.rbxm (14.5 KB)

Feel free to use/modify it for your game
Here’s video showing it

2 Likes