Hello. So i am making a rifle that fires laser balls basically. But when i shoot, the projectiles immediately explodes. Some times when i point my cursor to somewhere else the projectiles fly normally. I don’t understand why this is happening!
--//Services//--
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")
local Debris = game:GetService("Debris")
--//Variables//--
local TweenInform = TweenInfo.new(0.5, Enum.EasingStyle.Linear, Enum.EasingDirection.In)
local LaserRifle = script.Parent
local LaserBall = ReplicatedStorage.LaserBall
local Parts = LaserRifle.Parts
local LaserSpawn = Parts.LaserSpawn
local Hole = Parts.Hole
local HLEffect = Hole.Fire
local HLLight = Hole.SpotLight
local Handle = LaserRifle.Handle
local Select = Handle.Select
local Fire = Handle.Fire
local FireEvent = LaserRifle.Fire
local Explode = script.Explode
--//Function//--
local function onServerEvent(Player, MouseHit, Humanoid)
Fire:Play()
local Laser = LaserBall:Clone()
Laser.Flying.Playing = true
Laser.Parent = workspace
Laser.CFrame = CFrame.new(LaserSpawn.Position, MouseHit)
local Velocity = Instance.new("LinearVelocity", Laser)
Velocity.Attachment0 = Laser.Attachment
Velocity.MaxForce = math.huge
Velocity.VectorVelocity = CFrame.new(Laser.Position, MouseHit).LookVector * 50
Debris:AddItem(Laser, 5)
local function onLaserHit(Hit)
if Hit:IsA("BasePart") or Hit:IsA("Part") or
Hit:IsA("MeshPart") then
if Hit.Parent ~= LaserRifle then
Laser.CanTouch = false
Laser.CanCollide = false
local HitHum = Hit.Parent:FindFirstChild("Humanoid")
local ExPart = Instance.new("Part", workspace)
ExPart.Name = "ExSound"
ExPart.Material = Enum.Material.Neon
ExPart.Shape = Enum.PartType.Ball
ExPart.BrickColor = BrickColor.new("Cyan")
ExPart.Anchored = true
ExPart.CanTouch = false
ExPart.CanCollide = false
ExPart.Size = Vector3.new(.5, .5, .5)
ExPart.Transparency = 0
ExPart.Position = Laser.Position
local TweenSize = TweenService:Create(ExPart, TweenInform, {Size = Vector3.new(8, 8, 8)})
local TweenTrans = TweenService:Create(ExPart, TweenInform, {Transparency = 1})
TweenSize:Play()
TweenTrans:Play()
local ExSnd = Explode:Clone()
ExSnd.Parent = ExPart
ExSnd:Play()
Laser.CanTouch = false
task.wait(0.025)
Laser:Destroy()
task.wait(0.5)
ExPart:Destroy()
end
end
end
Laser.Touched:Connect(onLaserHit)
HLEffect.Enabled = true
HLLight.Enabled = true
task.wait(0.5)
HLEffect.Enabled = false
HLLight.Enabled = false
end
--//Connections//--
FireEvent.OnServerEvent:Connect(onServerEvent)
Here’s a video (WARNING, MAY BE LOUD FOR SOME) :