I want to fix something that lags my game. I have an archer that shoots arrows at targets but it causes lag.
Script:
Remote.Event:Connect(function(firePoint, hitPosition) -- Firepoint is an attachment
local g = Vector3.new(0,-ToolData.EffectGravity,0)
local x0 = firePoint.WorldPosition
local t = 1 -- time taking to destination
local v0 = (hitPosition - x0 - 0.5*g*t*t)/t
local projectile = Instance.new("Part")
projectile.Parent = workspace
local nt = 0
coroutine.wrap(function()
while (nt < t*2) do <------------------------------ lags game
if projectile ~= nil then
projectile:PivotTo(CFrame.new(0.5*g*nt*nt + v0*nt + x0, projectile.PrimaryPart.AssemblyLinearVelocity + projectile.PrimaryPart.Position))
end
nt = nt + RunService.Heartbeat:Wait()
end
if projectile ~= nil then
projectile:Destroy()
end
end)()
projectile.Touched:Connect(function(hit)
-- something
end)
game:GetService("Debris"):AddItem(projectile, 10)
end)
Part of this code was from this website by EgoMoose: Articles/Modeling a Projectile's Motion.md at master · EgoMoose/Articles · GitHub