- Note I am using old code (from Daxter33’s paintball game) and thus FilteringEnabled wasn’t a thing back then
I currently do this via the client, which works great, for creating the bullet on the client. However, I want bullets visible on all clients (so you can see bullets being fired at you)
However, I don’t think doing all this on the server is smart, as it may cause lag. So what should I be passing to the server, then back to the client?
--// Create the projectile
local function Projectile(weapon, start, target, damage, speed, drop, color)
local Collide = nil
local CollidePosition = nil
local Ratio = 10
local Direction = (target - start).Unit
local Hit, EndPosition = CastRay(start, target)
local Distance = (EndPosition - start).Magnitude
local BulletDrop = (drop / Ratio) / 250
local Length = speed / Ratio
local PrevPosition = start
-- Create the bullet object
local Bullet = Instance.new("Part")
Bullet.BrickColor = color
Bullet.Material = Enum.Material.SmoothPlastic
Bullet.Anchored = true
Bullet.CanCollide = false
Bullet.CFrame = CFrame.new(start, EndPosition)
Bullet.Parent = BulletsContainer
Debris:AddItem(Bullet, 5) -- Bullet should only last 5 seconds
if Distance > Length then
while true do
local BulletFront = (Bullet.CFrame * CFrame.new(0, 0, -1)).Position - Vector3.new(0, BulletDrop, 0)
local Direction = (BulletFront - Bullet.CFrame.Position).Unit
local Position = PrevPosition + (Direction * Length)
local Hit, EndPosition = CastRay(Bullet.CFrame.Position, BulletFront)
local NewDistance = (EndPosition - PrevPosition).Magnitude
CollidePosition = EndPosition
Collide = Hit
if NewDistance < Length then break end
local BulletDistance = (Position - PrevPosition).Magnitude
Bullet.Size = Vector3.new(0.5, 0.5, 5)
Bullet.CFrame = CFrame.new(PrevPosition, Position) * CFrame.new(0, 0, -BulletDistance / 2)
PrevPosition = Position
wait()
end
Bullet:Destroy() -- Destroy the bullet
else
print("WHY PRINT?")
local Position = PrevPosition + (Direction * Length)
local Hit, EndPosition = CastRay(start, Position)
local Distance = (EndPosition - start).Magnitude
Bullet.Size = Vector3.new(0.1, 0.1, Distance)
Bullet.CFrame = CFrame.new(start, EndPosition) * CFrame.new(0, 0, -Distance / 2)
Debris:AddItem(Bullet, 0.1) -- Remove bullet basically immediately
CollidePosition = EndPosition
Collide = Hit
end
CheckCollision(Collide, CollidePosition, color, damage, weapon) -- Bullet has hit something
return CollidePosition
end
--// Fire the bullet
function Bullet.Fire(weapon, start, target, damage, speed, drop, color)
coroutine.wrap(function()
Projectile(weapon, start, target, damage, speed, drop, color)
end)()
end