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.