Hey there!
I am trying to use beams as bullet effects when a gun goes off. The raycasting of the actual bullet is working well but the same cannot be said for the effects of the beam. I have set the CFrame of the attachment which controls the length of beam to be auto positioned according to the RayCast Results.
tool.Activated:Connect(function()
if debounce == false and ammo > 0 and Reloading == false then
debounce = true
game.Workspace.Sound["Silenced Pistol 1 (SFX)"]:Play()
ammo -=1
local Point = tool.Handle.Point
local PointLookVector = Point.CFrame.LookVector
local RaycastParams = RaycastParams.new()
RaycastParams.FilterDescendantsInstances = {tool.Parent}
RaycastParams.FilterType = Enum.RaycastFilterType.Exclude
local Point = tool.Handle.Point
local doDmgRE = tool:WaitForChild("DoDamage")
local RaycastResult = workspace:Raycast(Point.Position, PointLookVector * 500, RaycastParams)
--=======Bullet Effects=======================
local Debris = game:GetService("Debris")
local function BulletRay()
if RaycastResult == nil then
print("Jammed")
else
local RayDistance = RaycastResult.Distance
local RayPosition = RaycastResult.Position
local Silencer = tool.Handle.Silencer
local Att0 = Silencer.Attachment0
local Att1 = Silencer.Attachment1
local Beam = Silencer.Beam
local Att0Clone = Att0:Clone()
local Att1Clone = Att1:Clone()
local BeamClone = Beam:Clone()
Att0Clone.Parent = Silencer
Att1Clone.Parent = Silencer
BeamClone.Parent = Silencer
Debris:AddItem(BeamClone, 0.5)
Debris:AddItem(Att0Clone, 0.5)
Debris:AddItem(Att1Clone, 0.5)
Att1Clone.WorldPosition = RayPosition
Att0Clone.WorldPosition = Att0.WorldPosition
end
end
BulletRay()
--=======Damage RemoteEvent=======================
if RaycastResult then
local RaycastInstance = RaycastResult.Instance
local Findmodel = RaycastInstance:FindFirstAncestorOfClass("Model")
if Findmodel then
local FindPlayerHumanoid = Findmodel
if FindPlayerHumanoid:FindFirstChild("Humanoid") then
local targetPlayer = FindPlayerHumanoid
local dmgHumanoid = 100
doDmgRE:FireServer(targetPlayer, dmgHumanoid)
end
end
end
task.wait(0.15)
Player.PlayerGui["Ammo GUI"].Frame.TextLabel.Text = "Ammo: "..ammo.."/"..maxammo
debounce = false
elseif ammo <= 0 then
return
end
end)