It’s below the function that fires when the ray hits something, and above the function that fires the bullet. Here’s the entire script:
local fireEvent = script.Parent:WaitForChild("Fire")
local FastCast = require(game.ServerStorage:WaitForChild("FastCastRedux"))
local PartCache = require(game.ServerStorage.PartCache)
local origin = Vector3.zero
local direction = Vector3.zero
local bulletsFolder = workspace:FindFirstChild("BulletFolder") or Instance.new("Folder", workspace)
bulletsFolder.Name = "BulletFolder"
--make base bullet
local bulletTemplate = Instance.new("Part")
bulletTemplate.Anchored = true
bulletTemplate.CanCollide = false
bulletTemplate.Shape = "Ball"
bulletTemplate.Size = Vector3.new(0.2,0.2,0.2)
bulletTemplate.Material = Enum.Material.Neon
bulletTemplate.CastShadow = false
bulletTemplate.Color = Color3.new(1,1,0)
bulletTemplate.Transparency = 1
--make attachments for trail
local leftAttach = Instance.new("Attachment", bulletTemplate)
leftAttach.Position = Vector3.new(-0.15,0,0)
local rightAttach = Instance.new("Attachment", bulletTemplate)
rightAttach.Position = Vector3.new(0.15,0,0)
--trail itself
local trail = Instance.new("Trail", bulletTemplate)
trail.Color = ColorSequence.new{
ColorSequenceKeypoint.new(0, Color3.new(1,1,1)),
ColorSequenceKeypoint.new(0.03, Color3.new(1,1,1)),
ColorSequenceKeypoint.new(0.4, Color3.new(1,1,0.1)),
ColorSequenceKeypoint.new(0.75, Color3.new(1,0,0)),
ColorSequenceKeypoint.new(1, Color3.new(0,0,0))
}
trail.FaceCamera = true
trail.LightEmission = 1
trail.LightInfluence = 1
trail.Transparency = NumberSequence.new{
NumberSequenceKeypoint.new(0, 0),
NumberSequenceKeypoint.new(1, 1)
}
trail.Archivable = true
trail.Attachment0 = leftAttach
trail.Attachment1 = rightAttach
trail.Enabled = false
trail.Lifetime = 0.2
local caster = FastCast:new()
local rayParams = RaycastParams.new()
rayParams.FilterType = Enum.RaycastFilterType.Blacklist
rayParams.FilterDescendantsInstances = {script.Parent, bulletsFolder}
local bulletCache = PartCache.new(bulletTemplate, 100, bulletsFolder)
local behavior = FastCast:newBehavior()
behavior.Acceleration = Vector3.new(0, -workspace.Gravity, 0)
behavior.IgnoreWater = true
behavior.AutoIgnoreContainer = false
behavior.RaycastParams = rayParams
behavior.CosmeticBulletContainer = bulletsFolder
behavior.CosmeticBulletProvider = bulletCache
local function onRayHit(cast, result, velocity, bullet)
local hit = result.Instance
local character = hit:FindFirstAncestorWhichIsA("Model")
if character and character:FindFirstChild("Humanoid") then
if hit.Name == "Head" then
character.Humanoid:TakeDamage(50)
elseif hit.Name == "LeftUpperLeg" or hit.Name == "LeftLowerLeg" or hit.Name == "LeftFoot" or hit.Name == "RightUpperLeg" or hit.Name == "RightLowerLeg" or hit.Name == "RightFoot" then
character.Humanoid:TakeDamage(10)
else
character.Humanoid:TakeDamage(20)
end
end
delay(2, function()
bullet.Trail.Enabled = false
bulletCache:ReturnPart(bullet)
end)
end
local function onLengthChanged(cast, lastPoint, direction, length, velocity, bullet)
bullet.Trail.Enabled = true
if bullet then
bullet.CFrame = CFrame.new(lastPoint + (direction * length))
--bullet.Size = Vector3.new(0.2,0.2,math.abs(velocity.Magnitude)/25)
end
end
fireEvent.OnServerEvent:Connect(function(player, firePoint, fireDirection, ignore)
origin = firePoint
direction = (fireDirection - firePoint).Unit
rayParams.FilterDescendantsInstances = {script.Parent, game.Workspace:WaitForChild(ignore), bulletsFolder}
behavior.RaycastParams = rayParams
caster:Fire(origin, direction, 1000, behavior)
end)
caster.LengthChanged:Connect(onLengthChanged)
caster.RayHit:Connect(onRayHit)