Trying to make gun script

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)

Is this the line that is erroring? If so that means you didn’t send “HitPosition” thru as an argument, as the error states you are doing nil - Vector3

–Edit, Sorry lemme read the other portion of the code, mb
– Edit2, assuming rep.SoundEffect:FireServer(tip,'Universal','Jam',true,false) refers to the event in playClientSound, you are taking 7 arguments (minus 1 for the player argument) in the OnServerEvent, but only send 5 thru

1 Like

But I swear I did. Hit position is in fireserver (edit x2 im new to remote events still learning)

	rep.SoundEffect:FireServer(tip,'Hyke','Fire',true,false,HitPosition)

Sorry, re-edited my post. it was the rep.SoundEffect:FireServer(tip,'Universal','Jam',true,false) line above it where you didn’t send it.

1 Like
local aimDirection = (HitPosition - startPos).Unit * Range

Would appear to be the code where the error happens.
I think this is because here:

rep.SoundEffect:FireServer(tip,'Universal','Jam',true,false)

and here:

rep.SoundEffect:FireServer(tip,'Hyke','Fire',true,false,HitPosition)

You are only sending 5 and six arguments, but in this line:

playClientSound.OnServerEvent:connect(function(plr,part,weaponName,soundName,start,looped,specialCaseDistance,HitPosition)

You are receiving 7 arguments (not including the built-in player argument).

I hope this makes sense.

1 Like