Arrow with apply impulse lags the game

Hey! So im doing this little bow weapon, im very happy with it, but i wanted the arrow to be stuck in the ground or base parts if it hits them. So i tried stopping the ApplyImpulse() with AssemblyLinearVelocity = Vector3.zero, and then moving it to raycast position and anchoring it, which works fine, but tremendously lags the game, i have no idea why.

Video of the issue: https://youtu.be/2RiDFSs1NWY

Heres the bow script, that creates and launches the arrow:

-- Setting --

local Damage = 17
local cooldown = 0.5
local Chargetime = 1
local ArrowVelocity = 120
local minChargeTime = 0.5
local ArrowLife = 30

-- Animation Ids -

local HoldingAnimationId = "http://www.roblox.com/asset/?id=16185149749"
local ChargingAnimationId = "http://www.roblox.com/asset/?id=16186804217"
local ChargedAnimationId = "http://www.roblox.com/asset/?id=16185216323"
local FireAnimationId = "http://www.roblox.com/asset/?id=16184482608"


-- Variable --
local remote = script.Parent:WaitForChild("Update")
local LastFire = tick()
local ChargeStart = tick()
local tool = script.Parent
local Player = tool.Parent.Parent
local Character = Player.Character
local Humanoid = Character:FindFirstChild("Humanoid")
local isCharging = false
local Arrow =game.ReplicatedStorage:WaitForChild("Arrow")

-- Anims -- 
local Anim = Instance.new("Animation")
Anim.AnimationId = HoldingAnimationId
local HoldingAnim = Humanoid:LoadAnimation(Anim)

local Anim = Instance.new("Animation")
Anim.AnimationId = ChargingAnimationId
local CharginAnim = Humanoid:LoadAnimation(Anim)

local Anim = Instance.new("Animation")
Anim.AnimationId = ChargedAnimationId
local ChargedAnim = Humanoid:LoadAnimation(Anim)

local Anim = Instance.new("Animation")
Anim.AnimationId = FireAnimationId
local FireAnim = Humanoid:LoadAnimation(Anim)

local DrawSound = script.Parent.BowDraw
-- Script --

remote.OnServerEvent:Connect(function(plr, Type, LookVector, RightVector)
	if Type == "Unequipped" then
		print(("UNEQUIPPING BOW"))
		HoldingAnim.Looped = false
		DrawSound:Stop()
		HoldingAnim:Stop()
		HoldingAnim.Looped = false
		isCharging = false
		script.Parent.Arrow.Transparency = 1
		CharginAnim:Stop()
		ChargedAnim:Stop()
	end
	if plr.Name == tool.Parent.Name then
		if Type == 'Activated' then

			if tick()-LastFire > cooldown then
				script.Parent.Arrow.Transparency = 0
				ChargeStart = tick()
				isCharging = true
				HoldingAnim:Stop()
				CharginAnim:Play()
				DrawSound:Play()
				CharginAnim.Stopped:wait()
				ChargedAnim:Play()
			end
		elseif Type == "Deactivated" then
			DrawSound:Stop()
			script.Parent.Arrow.Transparency = 1
			HoldingAnim:Play()
			ChargedAnim:Stop()
			if tick()-ChargeStart > minChargeTime then

				local SoundEffect = Instance.new("Sound")
				SoundEffect.SoundId = "rbxassetid://173979241"
				SoundEffect.Name = "DeathSoundEffect"
				SoundEffect.Volume = 1
				SoundEffect.PlaybackSpeed = 1
				SoundEffect.SoundGroup = nil
				SoundEffect.PlayOnRemove = false
				SoundEffect.Looped = false
				SoundEffect.RollOffMaxDistance = 10000
				SoundEffect.RollOffMinDistance = 10
				SoundEffect.TimePosition = 0
				SoundEffect.Archivable = false
				SoundEffect.Parent = script.Parent:FindFirstChild("Union")
				SoundEffect:Play()

				--FireArrow
				local  A = Arrow:Clone()
				if not A then
					return
						else
					if A.Sender then
						A.Sender.Value = Character
						A.Parent = game.Workspace
						A.CFrame = script.Parent.Arrow.CFrame
						A.Position = A.Position + RightVector*2


						
					else
						A.Parent = game.Workspace
						A.CFrame = script.Parent.Arrow.CFrame 

						

					end 
				end
				
				

				-- Calculate Velocity and damage
				if tick()-ChargeStart < Chargetime then
					A:ApplyImpulse(LookVector*(tick()-ChargeStart)*ArrowVelocity)
					A.Damage.Value = Damage*(tick()-ChargeStart)
				else
					A.Damage.Value = Damage
					A:ApplyImpulse(LookVector*Chargetime*ArrowVelocity)
				end

				--Deletion

			end
			isCharging = false

		elseif Type == "Equipped" then	

			HoldingAnim:Play()
			HoldingAnim.Looped = true
		end
	end
	
end)

And heres the script inside of the arrow:

local Arrow = script.Parent
local Sender = Arrow.Sender
local Damage = Arrow.Damage
local RS = game:GetService("RunService")
local LastPosition = Arrow.Position
local Players = game:GetService("Players")
local candamage = true
function Hitmarker(hit,dmg)
	Players:FindFirstChild(Sender.Value.Name):FindFirstChild("PlayerGui").Markers.Wrapper:FindFirstChild("First").BackgroundTransparency = 0
	Players:FindFirstChild(Sender.Value.Name):FindFirstChild("PlayerGui").Markers.Wrapper:FindFirstChild("Second").BackgroundTransparency = 0
	Players:FindFirstChild(Sender.Value.Name):FindFirstChild("PlayerGui").Markers.Wrapper:FindFirstChild("Third").BackgroundTransparency = 0
	Players:FindFirstChild(Sender.Value.Name):FindFirstChild("PlayerGui").Markers.Wrapper:FindFirstChild("Fourth").BackgroundTransparency = 0
	Players:FindFirstChild(Sender.Value.Name):FindFirstChild("PlayerGui").Markers.Wrapper:FindFirstChild("Hit").TextTransparency = 0
	Players:FindFirstChild(Sender.Value.Name):FindFirstChild("PlayerGui").Markers.Wrapper:FindFirstChild("First").BackgroundColor3 = Color3.fromRGB(255, 255, 255)
	Players:FindFirstChild(Sender.Value.Name):FindFirstChild("PlayerGui").Markers.Wrapper:FindFirstChild("Second").BackgroundColor3 = Color3.fromRGB(255, 255, 255)
	Players:FindFirstChild(Sender.Value.Name):FindFirstChild("PlayerGui").Markers.Wrapper:FindFirstChild("Third").BackgroundColor3 = Color3.fromRGB(255, 255, 255)
	Players:FindFirstChild(Sender.Value.Name):FindFirstChild("PlayerGui").Markers.Wrapper:FindFirstChild("Fourth").BackgroundColor3 = Color3.fromRGB(255, 255, 255)
	Players:FindFirstChild(Sender.Value.Name):FindFirstChild("PlayerGui").Markers.Wrapper:FindFirstChild("Hit").Text = hit.Parent.Name.."'s "..hit.Name.." for \n"..math.floor(dmg)

end
function HitSound(parent)
	local SoundEffect = Instance.new("Sound")
	SoundEffect.SoundId = "rbxassetid://5473058688"
	SoundEffect.Name = "DeathSoundEffect"
	SoundEffect.Volume = 1
	SoundEffect.PlaybackSpeed = 1
	SoundEffect.SoundGroup = nil
	SoundEffect.PlayOnRemove = false
	SoundEffect.Looped = false
	SoundEffect.RollOffMaxDistance = 10000
	SoundEffect.RollOffMinDistance = 10
	SoundEffect.TimePosition = 0
	SoundEffect.Archivable = false
	SoundEffect.Parent = parent
	SoundEffect:Play()
end
function ImpactSound(parent)
	local SoundEffect = Instance.new("Sound")
	SoundEffect.SoundId = "rbxassetid://16185680893"
	SoundEffect.Name = "DeathSoundEffect"
	SoundEffect.Volume = 1
	SoundEffect.PlaybackSpeed = 1
	SoundEffect.SoundGroup = nil
	SoundEffect.PlayOnRemove = false
	SoundEffect.Looped = false
	SoundEffect.RollOffMaxDistance = 10000
	SoundEffect.RollOffMinDistance = 10
	SoundEffect.TimePosition = 0
	SoundEffect.Archivable = false
	SoundEffect.Parent = parent
	SoundEffect:Play()
end
RS.Heartbeat:Connect(function()
	local Ignore = {Sender.Value, Arrow}
	for i, Part in pairs(workspace:GetDescendants()) do
		if Part:IsA("BasePart") then
			if not Part.Parent:FindFirstChildOfClass("Humanoid") and not Part.CanCollide then
				table.insert(Ignore, Part)
			end
		end
	end
	local params = RaycastParams.new()
	params.FilterDescendantsInstances = Ignore
	params.FilterType = Enum.RaycastFilterType.Blacklist



	local results = workspace:Raycast(LastPosition, Arrow.Position-LastPosition, params)
	if results then
		print("INSTANCE: "..results.Instance.Name)
		if results.Instance.Parent:FindFirstChildOfClass("Humanoid") then
			local hum = results.Instance.Parent:FindFirstChildOfClass("Humanoid")
			local hit = results.Instance
			local dmg = hit:FindFirstChild("dmgMULT")
			if candamage == true then
				candamage = false
				task.wait(0.1)
				hum.Health = hum.Health - (dmg.Value*Damage.Value)
				Hitmarker(hit, (dmg.Value*Damage.Value))
				ImpactSound(hit)
			end
			else
			Arrow.AssemblyLinearVelocity = Vector3.zero
			candamage = false
			Arrow.Position = results.Position
			Arrow.Anchored = true
		end


	end
	LastPosition = Arrow.Position
end)

probably because of this (not sure) cuz it gets all the parts and if theres too much it might actually cause lag

or maybe this but i cant really help in that much stuff

1 Like

You didn’t call :Disconnect on the RunService and the prints probably cause lag too.