Hello devforum users I have encountered this error
attempt to perform arithmetic (sub) on nil and Vector3
While I was trying to make a script for a gun that supposedly just makes a laser wherever your mouse points at. I never was able to find a solution or get help on devforum through posts so I would appreciate it being fixed.
Serverscript
playClientSound.OnServerEvent:connect(function(plr,part,weaponName,soundName,start,looped,specialCaseDistance,HitPosition)
playClientSound:FireAllClients(part,'Weapon'..weaponName,soundName,start,looped,specialCaseDistance)
if soundName=='Fire' then
local rayarams = RaycastParams.new()
rayarams.FilterDescendantsInstances = {plr.Character}
rayarams.FilterType = Enum.RaycastFilterType.Blacklist
local startPos = plr.Character.PrimaryPart.Position
local Range = 300
local aimDirection = (HitPosition - startPos).Unit * Range
local RayResult = workspace:Raycast(startPos, aimDirection, rayarams)
if RayResult then
local hitPart = RayResult.Instance
local hitPos = RayResult.Position
local distance = (hitPos - startPos).Magntiude
local laserPart = Instance.new("Part")
laserPart.Anchored = true
laserPart.Name = "Laser"
laserPart.BrickColor = BrickColor.new("Really red")
laserPart.CanCollide = false
laserPart.Size = Vector3.new(0.2, 0.2, distance)
laserPart.CFrame = CFrame.new(part.Handle.Position, hitPos) * CFrame.new(0, 0, -distance / 2)
laserPart.Parent = workspace
end
Client script
local mouseLocation = getWorldMousePosition()
local targetDirection = (mouseLocation - tool.Handle.Position).Unit
local directionVector = targetDirection * MAX_MOUSE_DISTANCE
local weaponRaycastParams = RaycastParams.new()
weaponRaycastParams.FilterDescendantsInstances = {game.Players.LocalPlayer.Character}
local weaponRaycastResult = workspace:Raycast(tool.Handle.Position, directionVector, weaponRaycastParams)
local HitPosition = weaponRaycastResult.Position
if active and not firing and not jammed and not holster and not chilling and clip>0 then
tool:WaitForChild('Handle')
firing=true
if math.random(1,jamChance)==1 then
jammed=true
firing=false
rep.SoundEffect:FireServer(tip,'Universal','Jam',true,false)
return
end
rep.SoundEffect:FireServer(tip,'Hyke','Fire',true,false,HitPosition)