the code mostly isn’t mine I just used a tutorial to get a hang of the basics ( The Most ACCURATE Projectile in ROBLOX!! - YouTube
module:
local Projectiles = {}
local RunService = game:GetService("RunService")
local Debris = game:GetService("Debris")
Projectiles.new = function(Lifetime, Gravity, Params)
local Projectile = {}
Projectile.Lifetime = Lifetime
Projectile.Gravity = Gravity
Projectile.Params = Params
function Projectile:Cast(Origin, Destination, Velocity, Deviation, Vizualizer)
Vizualizer = Vizualizer:Clone()
Vizualizer.Parent = workspace
Vizualizer.Position = Origin
local offset = math.random(-Deviation,Deviation)
local offset2 = math.random(-Deviation,Deviation)
local offset3 = math.random(-Deviation,0)
local vForce = (Destination - Origin).Unit * Velocity
local a = Vector3.new(offset, self.Gravity + offset3, offset2)
local t = 0
local currentPos = Origin
local rayResult = nil
local hit = false
local Connection = RunService.Heartbeat:Connect(function(dt)
t += dt
local newPos = Vector3.new(
Origin.X + vForce.X * t + 0.5*a.X*t*t,
Origin.Y + vForce.Y * t + 0.5*a.Y*t*t,
Origin.Z + vForce.Z * t + 0.5*a.Z*t*t
)
--[[
local tracker = Instance.new("ConeHandleAdornment")
tracker.Parent = workspace
tracker.Adornee = workspace.Terrain
tracker.CFrame = CFrame.lookAt(currentPos,newPos)
tracker.Height = (currentPos - newPos).Magnitude
Debris:AddItem(tracker,3)
]]
rayResult = workspace:Raycast(currentPos, newPos - currentPos, Params)
currentPos = newPos
Vizualizer.Position = currentPos
if rayResult or t > self.Lifetime then
hit = true
Vizualizer:Destroy()
end
end)
while not hit do
task.wait()
end
Connection:Disconnect()
end
return Projectile
end
return Projectiles
test client script:
local root = script.Parent:WaitForChild("HumanoidRootPart")
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local params = RaycastParams.new()
params.FilterDescendantsInstances = {script.Parent}
local projectiles = require(game.ReplicatedStorage.Projectiles)
local projectile = projectiles.new(8,-196.2, params)
mouse.Button1Down:Connect(function()
for i = 1,9 do
projectile:Cast(root.Position, mouse.Hit.Position, 1573,100, game.ReplicatedStorage.TracerVizualizer)
end
end)
for example im firing a shotgun and want to cast 9 projectiles, the next cast has to wait until the first cast finishes because of the task.wait() in the module. what ways are there to solve this without using coroutines?