Hello devforum,
I’m making a gun that shoots parts towards the cursor, and while troubleshooting i’ve found a piece of code that does exactly what I want my gun to do (Blue Hyperlaser)
I can’t seem to wrap my head around it, can you guys break down and explain it to me?
THE CODE OF INTEREST:
function Activated(target)
if Tool.Enabled and Humanoid.Health > 0 then
Tool.Enabled = false
InvokeClient("PlaySound", Sounds.Fire)
local HandleCFrame = Handle.CFrame
local FiringPoint = HandleCFrame.p + HandleCFrame:vectorToWorldSpace(NozzleOffset)
local ShotCFrame = CFrame.new(FiringPoint, target)
local LaserShotClone = BaseShot:Clone()
LaserShotClone.CFrame = ShotCFrame + (ShotCFrame.lookVector * (BaseShot.Size.Z / 2))
local BodyVelocity = Instance.new("BodyVelocity")
BodyVelocity.velocity = ShotCFrame.lookVector * Speed
BodyVelocity.Parent = LaserShotClone
LaserShotClone.Touched:connect(function(Hit)
if not Hit or not Hit.Parent then
return
end
Touched(LaserShotClone, Hit)
end)
Debris:AddItem(LaserShotClone, Duration)
LaserShotClone.Parent = game:GetService("Workspace")
wait(0.6) -- FireSound length
InvokeClient("PlaySound", Sounds.Reload)
wait(0.75) -- ReloadSound length
Tool.Enabled = true
end
end
function Unequipped()
end
BaseShot = Instance.new("Part")
BaseShot.Name = "Effect"
BaseShot.BrickColor = BrickColor.new("Toothpaste")
BaseShot.Material = Enum.Material.Plastic
BaseShot.Shape = Enum.PartType.Block
BaseShot.TopSurface = Enum.SurfaceType.Smooth
BaseShot.BottomSurface = Enum.SurfaceType.Smooth
BaseShot.FormFactor = Enum.FormFactor.Custom
BaseShot.Size = Vector3.new(0.2, 0.2, 3)
BaseShot.CanCollide = false
BaseShot.Locked = true
SelectionBoxify(BaseShot)
Light(BaseShot)
BaseShotSound = Sounds.HitFade:Clone()
BaseShotSound.Parent = BaseShot
Tool.Equipped:connect(Equipped)
Tool.Unequipped:connect(Unequipped)
MY CODE:
local tool = script.Parent
local sound = script.Parent.Sound
game:GetService(“ReplicatedScriptService”)
local shootevent = game.ReplicatedStorage:WaitForChild(“ShootEvent”)
local function serverfired(LOC, LOC2)
local playerworkspace = workspace:FindFirstChild(tostring(LOC))
local bullet = Instance.new("Part", workspace)
bullet.Shape = "Ball"
bullet.Position = playerworkspace.PelletGun.Handle.Position
bullet.CanCollide = false
bullet.Size = Vector3.new(1, 1, 1)
local antigravityforce = Instance.new("BodyForce", bullet)
antigravityforce.Force = Vector3.new(0, 80, 0)
local force = Instance.new("BodyForce", bullet)
force.Force = Vector3.new(1000, 0, 0)
sound:Play()
wait(1)
bullet:Destroy()
end
shootevent.onServerEvent:Connect(serverfired)
(I am currently having trouble formatting code, please excuse the messy formatting)