Need help with raycast (Guns)

Hello!

Im very new to using raycasting and well I wanted to make a gun so yea… But… my gun is not working.

The problem is that when I click it fires a ray but it doesn’t hit the player even though my mouse is pointing at it.

My guess is that the ray is going in another direction than the mouse but i’m not sure.

THis is my localscriptL

local player = game.Players.LocalPlayer

local camera = game.Workspace.CurrentCamera

local rs = game:GetService("RunService")

local tool = script.Parent
local event = script.Parent:WaitForChild("shotEvent")
local showDamageEvent = event:WaitForChild("showDamage")

local char = player.Character or player.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")

local HRP = char:WaitForChild("HumanoidRootPart")

local gunPoint = script.Parent:WaitForChild("Animations").gunPoint
local shotAnim = script.Parent:WaitForChild("Animations").shotAnim
local track1 = animator:LoadAnimation(gunPoint)
local track2 = animator:LoadAnimation(shotAnim)

local mouse = player:GetMouse()

local debounce = false



local uis = game:GetService("UserInputService")

local equipRemote = script.Parent:WaitForChild("EquipRemote")

tool.Equipped:Connect(function()
	
	
	
	equipRemote:FireServer()
	track1.Looped = true
	
	track1:Play()
	
	
	function shiftLock(active)
		if active then
			
			
			
			humanoid.AutoRotate = false 
			

			humanoid.CameraOffset = Vector3.new(1.75,0,0)
			player.CameraMinZoomDistance = 4
			player.CameraMaxZoomDistance = 4
			
			
			
			game:GetService("RunService"):BindToRenderStep("ShiftLock", Enum.RenderPriority.Character.Value, function()
				game:GetService("UserInputService").MouseBehavior = Enum.MouseBehavior.LockCenter
				---------------------------------------------------------
				local _, y = workspace.CurrentCamera.CFrame.Rotation:ToEulerAnglesYXZ()
				HRP.CFrame = CFrame.new(HRP.Position) * CFrame.Angles(0,y,0)
				---------------------------------------------------------
			end) 
		else
			humanoid.AutoRotate = true 
			humanoid.CameraOffset = Vector3.new(0,0,0) 
			rs:UnbindFromRenderStep("ShiftLock")
			uis.MouseBehavior = Enum.MouseBehavior.Default
		end
	end
	
	shiftLock(true)
	
		

	------------------------------------
	
	tool.Activated:Connect(function()
		if debounce == false then
			debounce = true
			
			track2:Play()
			track1:Stop()
			
			
			local tool_Mesh = tool:WaitForChild("Mesh")
			
			local params = RaycastParams.new()
			params.FilterType = Enum.RaycastFilterType.Exclude
			params.FilterDescendantsInstances = {char}

			local direction_ = camera:ScreenPointToRay(0.5,0.5).Direction
			local origin_ = char:WaitForChild("HumanoidRootPart").CFrame.LookVector * 40
			
			local result = workspace:Raycast(origin_, direction_, params)
			
			event:FireServer(result)
			
			track2.Stopped:Wait()
			track1:Play()
			debounce = false
			
		end
		
		
		
	end)
end)

tool.Unequipped:Connect(function()
	track2:Stop()
	track1:Stop()
	shiftLock(false)
	player.CameraMinZoomDistance=20
	player.CameraMaxZoomDistance=20
	wait()
	player.CameraMinZoomDistance=0.5
	player.CameraMaxZoomDistance=128
end)

And this is my serverscript:

local shotRemote = script.Parent:WaitForChild("shotEvent")
local showDamageEvent = shotRemote:WaitForChild("showDamage")



shotRemote.OnServerEvent:Connect(function(player, result)
	
	local char = player.Character or player.CharacterAdded:Wait()
	local humanoid = char:WaitForChild("Humanoid")
	
	
	
	
	print(result)
	if result then
		print(result)
		local damage = 20
		local part = result.Instance
		
		local enemy = part.Parent:FindFirstChild("Humanoid") or part.Parent.Parent:FindFirstChild("Humanoid")
		
		if enemy and enemy:GetSate() ~= Enum.HumanoidStateType.Dead then
			showDamageEvent:FireClient(player, enemy.Parent)
			enemy:TakeDamge(damage)
		end
	end
	
	
	
end)

On line 14 in the serversciprt I have a print statement and it outputs “nil” so everything in the if result then code will not be executed since result is nil

I hope you can help me with this!

1 Like

I wonder whether it is the ray origin that is affecting it. Secondly, your ray direction is strange, but might be OK, dunno.

I use the following reliably on both pc and mobile devices:

local mouseHit = mouse.Hit.p
local origin_= head.Position
local direction_ = (mouseHit - rayOrigin).Unit * 200  -- limits ray range to 200 units, change it if you like
local result = workspace:Raycast(rayOrigin, rayDirection, rayParam)
print("RAY", result)

Local script:

local player = game.Players.LocalPlayer

local camera = game.Workspace.CurrentCamera

local rs = game:GetService("RunService")

local tool = script.Parent
local event = script.Parent:WaitForChild("shotEvent")
local showDamageEvent = event:WaitForChild("showDamage")

local char = player.Character or player.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")

local HRP = char:WaitForChild("HumanoidRootPart")

local gunPoint = script.Parent:WaitForChild("Animations").gunPoint
local shotAnim = script.Parent:WaitForChild("Animations").shotAnim
local track1 = animator:LoadAnimation(gunPoint)
local track2 = animator:LoadAnimation(shotAnim)

local mouse = player:GetMouse()

local debounce = false



local uis = game:GetService("UserInputService")

local equipRemote = script.Parent:WaitForChild("EquipRemote")

tool.Equipped:Connect(function()
	
	
	
	equipRemote:FireServer()
	track1.Looped = true
	
	track1:Play()
	
	
	function shiftLock(active)
		if active then
			
			
			
			humanoid.AutoRotate = false 
			

			humanoid.CameraOffset = Vector3.new(1.75,0,0)
			player.CameraMinZoomDistance = 4
			player.CameraMaxZoomDistance = 4
			
			
			
			game:GetService("RunService"):BindToRenderStep("ShiftLock", Enum.RenderPriority.Character.Value, function()
				game:GetService("UserInputService").MouseBehavior = Enum.MouseBehavior.LockCenter
				---------------------------------------------------------
				local _, y = workspace.CurrentCamera.CFrame.Rotation:ToEulerAnglesYXZ()
				HRP.CFrame = CFrame.new(HRP.Position) * CFrame.Angles(0,y,0)
				---------------------------------------------------------
			end) 
		else
			humanoid.AutoRotate = true 
			humanoid.CameraOffset = Vector3.new(0,0,0) 
			rs:UnbindFromRenderStep("ShiftLock")
			uis.MouseBehavior = Enum.MouseBehavior.Default
		end
	end
	
	shiftLock(true)
	
		

	------------------------------------
	
	tool.Activated:Connect(function()
		if debounce == false then
			debounce = true
			
			track2:Play()
			track1:Stop()
			
			local mouseHit = mouse.Hit.Position
			
			local tool_Mesh = tool:WaitForChild("Mesh")
			
			local params = RaycastParams.new()
			params.FilterType = Enum.RaycastFilterType.Exclude
			params.FilterDescendantsInstances = {char}

			
			local origin_ = char:WaitForChild("Head").Position
			local direction = (mouseHit - origin_).Unit * 200
			
			
			local result = workspace:Raycast(origin_, direction, params)
			
			event:FireServer(result)
			
			track2.Stopped:Wait()
			track1:Play()
			debounce = false
			
		end
		
		
		
	end)
end)

tool.Unequipped:Connect(function()
	track2:Stop()
	track1:Stop()
	shiftLock(false)
	player.CameraMinZoomDistance=20
	player.CameraMaxZoomDistance=20
	wait()
	player.CameraMinZoomDistance=0.5
	player.CameraMaxZoomDistance=128
end)

Serverscript:

local shotRemote = script.Parent:WaitForChild("shotEvent")
local showDamageEvent = shotRemote:WaitForChild("showDamage")



shotRemote.OnServerEvent:Connect(function(player, result)
	
	local char = player.Character or player.CharacterAdded:Wait()
	local humanoid = char:WaitForChild("Humanoid")
	
	
	
	
	print(result)
	if result then
		print(result)
		local damage = 20
		local part = result.Instance
		
		local enemy = part.Parent:FindFirstChild("Humanoid") or part.Parent.Parent:FindFirstChild("Humanoid")
		
		if enemy and enemy:GetSate() ~= Enum.HumanoidStateType.Dead then
			showDamageEvent:FireClient(player, enemy.Parent)
			enemy:TakeDamge(damage)
		end
	end
	
	
	
end)

And well i edited the code to what you send atleast I think don’t really fully understand your variables… Anyways it didn’t worked

Players.Player2.Backpack.Gun.fireGun_%:14: attempt to perform arithmetic (sub) on nil and Vector3

Im getting that error even though the whole scripts are working now… The player is getting damaged when I hit it in the head or in another body part. And only if my mouse is on it. So well how do I remove that error so I won’t get any errors from it.

Which line is it erroring at now?

Line 14 in the serverscript and I send you the 2 edited scripts. Which work but I want to get rid of that error

I’m not really sure which line is #14 in the script. Can you show me the actual line and where it gets the values from.
You could try and print the values before that calculation as from the error, it sounds like one of the values has not been assigned correctly or is not found.