Argument 2 missing or nil

Hello im having problem with my raycast script i had tried to solve this all day but there was no news

Server side :

remoteEvent.OnServerEvent:Connect(function(endPosition, startPosition, mouseHit, Gunmodel,pos,normal,player)
	local rayDirection = (mouseHit - startPosition).unit -- the direction of the raycast
	local direction = workspace:Raycast(startPosition - rayDirection * 1000) -- error happen
	local raycast = game.Workspace:Raycast(startPosition, direction)
	
	
	if direction then
		local hitPart = direction.Instance
		print(hitPart.Name)
	end

Cilent :

		local mouseHit = mouse.Hit.Position
			
		local startPosition = Gunmodel.Start.Position
		local endPosition = mouse.Hit.Position
		
	RelicatedStorage.Folder.AK47event:FireServer(endPosition, startPosition, mouseHit, Gunmodel,pos,normal,player)

Output :

Screenshot 2023-11-04 161455

Hope anyone can see this and help me im really mad rn :blush:

2 Likes
	local direction = workspace:Raycast(startPosition - rayDirection * 1000) -- error happen

doesnt always return a ray you need to make a check for it

1 Like

IIRC, raycast needs a position AND a direction, both as vector3.
You need to add a second parameter which is a direction

1 Like

Could you give me example ? This kinda hard for me as a newbie

Why is direction a RaycastResult? Shouldn’t it be a Vector3?

I am very confuse now this should work but the second error happen

– ServerScriptService.Ak45:7: attempt to perform arithmetic (sub) on Instance and Vector3 - Server - Ak45:7 –

Cilent (almost all script ingore some)

	if Ammo.Value > 0 then
		shooting = true
	end

	equipped = true

	mouseConnection = mouse.Button1Down:Connect(function()


		shooting = true

		while shooting == true and equipped == true and reloading == false and Ammo.Value > 0 do
			
		local mouseHit = mouse.Hit.Position
			
		local startPosition = Gunmodel.Start.Position
		local endPosition = mouse.Hit.Position
			
		local r = Ray.new(Camera.CFrame.Position, (mouse.Hit.Position - Camera.CFrame.Position).Unit * 500)
		

		
		-- Create a RaycastParams object and set CanCollide to false
		local raycastParams = RaycastParams.new()
		raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

		-- Add the CanCollide part to the IgnoreList

		-- Perform the raycast
		local part, pos, normal = workspace:FindPartOnRayWithIgnoreList(r, {player.Character}, false, false)

		raycastParams.FilterDescendantsInstances = {part}
		
			
		RelicatedStorage.Folder.AK47event:FireServer(endPosition, startPosition, mouseHit, Gunmodel,pos,normal,player)
			
			


			Ammo.Value = Ammo.Value -1
			coroutine.wrap(function()
				local Connection1
				local Connection2
				Connection1 = RS.RenderStepped:Connect(PlayRecoil)
				wait(0.01)
				Connection1:Disconnect()
				wait(0.05)
				-- recover recoil
				Connection2 = RS.RenderStepped:Connect(RecoverRecoil)
				wait(0.1)
				Connection2:Disconnect()

			end) ()

			mouse.Button1Up:Connect(function()
				-- release button here	
				shooting = false
				if shooting == true then
					shooting = false
				end
			end)
			wait(0.1)
		end
	end)

Server :

remoteEvent.OnServerEvent:Connect(function(endPosition, startPosition, mouseHit, Gunmodel,pos,normal,player)
	local direction = (endPosition - startPosition).Unit * 100

	local raycast = game.Workspace:Raycast(startPosition, direction)
	
	

	Gunmodel.Fire:Play()

	local part = Instance.new("Part")
	local decal = Instance.new("Decal")
	local sound = Instance.new("Sound")
	part.Size = Vector3.new(0.2, 0.2, 0.0001)

	decal.Texture = "rbxassetid://15128977943"--rbxassetid://7350477734 wall sound
	part.BrickColor = BrickColor.new("Yellow flip/flop")
	part.CanCollide = false
	part.Anchored = true
	part.Parent = visualsFolder
	part.Transparency = 1
	part.Position = endPosition --- remove this ----
	part.CFrame = CFrame.new(pos, pos + normal)
	decal.Parent = part
	sound.Parent = part
	sound.SoundId = "rbxassetid://15129408969"
	sound.Volume = 1
	sound.TimePosition = 0.3
	sound:Play()



	if raycast then
		local hitCharacter = raycast.Instance.Parent
		local humanoid = hitCharacter:FindFirstChild("Humanoid")
		if humanoid then
			if hitCharacter.Name ~= player.Character.Name then

				Gunmodel.HitSound:Play()
				if raycast.Instance.Name == "Head" then
					humanoid.Health -= 30
				else

					humanoid.Health -= math.random(20,26)
				end
			end
		end
	end
	wait(0.2)
	Gunmodel.Start.Flame.Enabled = false
	Gunmodel.Start.Flame1.Enabled = false
	Gunmodel.Start.Glow.Enabled = false
	Gunmodel.Start.Flash.Enabled = false
	Gunmodel.Start.MuzzleEffect.Enabled = false
	wait(3)
	part:Destroy()

end)

There is one point worth noting that “GunModel” i mention in script are from local script which mean it only 1 player see ? Doesnt it affect to the code i had tried the code inside tool and it work correctly just replace Gunmodel to Tool May i right ?