The ability projectiles in-game fly very slowly, and it causes CPU lag for some reason. I use BodyVelocities to make the projectiles move, but for some reason they go really slowly, and lag the game. I’ve tried looking at the scripts creating the projectile, but this is all it is.
local Beam = script.MagicBeam:Clone()
Beam.CFrame = Character:WaitForChild("RightHand").CFrame
Beam.Orientation = Character:WaitForChild("HumanoidRootPart").Orientation
Beam.Parent = workspace
Debris:AddItem(Beam,3)
local Vel = Instance.new("BodyVelocity",Beam)
Vel.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
Vel.Velocity = Character:WaitForChild("HumanoidRootPart").CFrame.lookVector * 75 --Speed
Beam.Fire:Play()
Beam.Touched:Connect(function(Hit)
if not Hit:IsDescendantOf(Character) and not game.Players:GetPlayerFromCharacter(Hit.Parent) then
if Hit:IsA("Part") or Hit:IsA("MeshPart") then
local Humanoid = Hit.Parent:FindFirstChild("Humanoid")
if Humanoid and not Humanoid:FindFirstChild(plr.Name..""..ability.Parent.Name) then
local tag = Instance.new("StringValue",Humanoid)
tag.Name = plr.Name..""..ability.Parent.Name
game.Debris:AddItem(tag,1)
weaponMod.DamageEnemy(plr,Hit.Parent,ability.Scales.Value,ability)
end
end
end
end)
It could be the touch detection, or just how I’ve set up the velocity, but I’m not sure. Any help would be appreciated!
It’s just a module script that has the TakeDamage function in it, it takes in other parameters, too, just so I don’t have to modify every takedamage function when I need to fix it. The beam doesn’t even have to hit an enemy for it to cause lag, either.
If you were a Physic nerd you’d know that while a Velocity goes forward, it gains energy. If you drop something off a building it will get faster, and faster, until it eventually hits the ground.
The problem with turning it down is that it doesn’t fall like an arrow, it’s supposed to have 0 drag, so it goes on forever until it gets destroyed. Is there a way to see which functions/scripts are causing lag? If it is, I can check if it’s not that script, but something related.
here is a way to make bullets that dont fall down and only go straight
-- studs per second
local speed = 5
-- where the bullet will start and the direction it will move to
local cFrame = CFrame.lookAt(Vector3.new(0, 10, 0), Vector3.new(0, 10, -100))
-- create a part
local part = Instance.new("Part")
part.CFrame = cFrame
part.AssemblyLinearVelocity = cFrame.LookVector * speed
part.Parent = game.Workspace
part.Touched:Conect(Function(OtherPart)
part:Destroy()
end)
-- destory part after 30 seconds if it does not hit anything
game.Debris:AddItem(part, 30)
-- add a attachment to the part
local attachment = Instance.new("Attachment")
attachment.Parent = part
-- add vectorForce that will counteract gravity so the bullet moves in a straight path
local vectorForce = Instance.new("VectorForce")
vectorForce.Attachment0 = attachment
vectorForce.RelativeTo = enum.ActuatorRelativeTo.World
vectorForce.Force = Vector3.new(0, game.Workspace.Gravity * part.AssemblyMass, 0)
vectorForce.Parent = part
I know it’s been a while since I’ve said anything, it’s just because I gave up for a while and worked on other things.
Anyway, I commented out the touched function, and that seems to be the only thing causing lag. The projectiles themselves don’t actually cause any lag. I don’t know what it is about the touched function, because I use the same function for the sword swinging damage function. It might be that the projectile is triggering the function too many times, but I’m not sure.