Raycast detect a humanoid and reduce its damage

The code below is from part of a ServerScript that is being fired when a RemoteEvent is triggered. How do I make it work?

local Params = RaycastParams.new()
Params.FilterDescendantsInstances = {layer.Character}
Params.FilterType = Enum.RaycastFilterType.Exclude

local raycast = workspace:Raycast(Tool.Handle.Position, rayDesti, Params)

if raycast then
	if raycast.Instance.Parent:FindFirstChild("Humanoid") then
		local humanoid = raycast.Instance.Parent:FindFirstChild("Humanoid")
		humanoid.Health -= 20
	end
end
8 Likes

You will need to identify a direction and distance for the raycast. Also not sure, but “player” may be mispelled as layer. Give the below code a try:

local rayDirection = Vector3.new(0, 0, -10) -- change to what direction and distance you want the ray to go
local rayDesti = Tool.Handle.Position + rayDirection -- this is the destination, needs initialization from position of tool

local Params = RaycastParams.new()
Params.FilterDescendantsInstances = {player.Character} -- I am assuming player was misspelled
Params.FilterType = Enum.RaycastFilterType.Exclude

local raycast = workspace:Raycast(Tool.Handle.Position, rayDesti, Params)

if raycast then
	if raycast.Instance.Parent:FindFirstChild("Humanoid") then
		local humanoid = raycast.Instance.Parent:FindFirstChild("Humanoid")
		humanoid.Health -= 20
	end
end
2 Likes

It’s still not doing damage.

1 Like

before I try and debug, did you change this line appropriately to your needs?

local rayDirection = Vector3.new(0,0,0) -- you need to change this according to your needs

If you need help with this, let me know and see how the tool is oriented.

If you already changed it and are sure the rayDirection is correct, let me know.

1 Like

I’m quite certain that everything is correct.

The rayDirection is set to the Mouse argument of the function.

image

and then in an event. It is used to set the direction it is to go in.

1 Like

One more question, could you tell me what type of form is Mouse in (Vector3?). If I am not mistaken, I can’t quite tell if Mouse is a Vector3 value. If it is, that is good. Also make sure that if you are using something like a unit vector for the mouse aim, to multiply that by as much as you need so that the weapon has depth and range.

If you dont have a vector3 for the mouse, perhaps try using ScreenPointToRay, as follows:

local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local Workspace = game:GetService("Workspace")

local player = Players.LocalPlayer
local camera = Workspace.CurrentCamera
local mouse = UserInputService:GetMouseLocation()

local rayDirection = (camera:ScreenPointToRay(mouse.x, mouse.y).Direction * 50) -- whatever number scale besides 50
local rayDesti = rayOrigin + rayDirection

-- the rest of the raycast code

If everything here is done, and tried, and still not working, let me know.

The mouse is a Vector3 I believe.

1 Like

Are you only using Mouse for rayDirection? For a mouse aimed situation while using mouse.Hit.Position, you would need to be subtracting the tool position from mouse.Hit.Position.

This is how the line should be, give it a try:

rayDirection = Mouse - Tool.Handle.Position
rayDesti = Tool.Handle.Position + rayDirection

Let me know if that works or not.

Still nothing…

I feel this might have to do with getting the raycast’s instance parent and the checking for the humanoid. Idk.

Hmmm… thats a bit strange. Can you confirm that you shot more than 4 times at the humanoid if it has a conventional health bar of 100 points?

I will get a response back to you in the meantime of what else it could be… ill need to look again.

1 Like

Try removing Tool.Handle.Position from rayDesti = Tool.Handle.Position + rayDirection, like this

rayDesti = rayDirection

I think I may have double counted the presence of the tool, as it is already established as the first raycast parameter.

Still no damage.

1 Like

Ok, were gonna attempt a last ditch effort to see the problem. Have this as the code:

print("mouse position: " .. tostring(Mouse)) 

local rayDirection = Mouse - Tool.Handle.Position
print("ray direction: " .. tostring(rayDirection))

local rayDesti = rayDirection
print("ray desti: " .. tostring(rayDesti))

local Params = RaycastParams.new()
Params.FilterDescendantsInstances = {player.Character}
Params.FilterType = Enum.RaycastFilterType.Exclude

local raycast = workspace:Raycast(Tool.Handle.Position, rayDesti, Params) 

if raycast then
	print("raycast hit something")
	if raycast.Instance.Parent:FindFirstChild("Humanoid") then
		print("humanoid found") -- Print if Humanoid found
		local humanoid = raycast.Instance.Parent:FindFirstChild("Humanoid")
		humanoid.Health -= 20
	else
		print("humanoid not found")
	end
else
	print("raycast did not hit anything")
end

Tell me what the console prints.

1 Like

I am sorry but could you try a video with the donsole visible as you shoot multiple bullets from different positions? I feel like I can get a better idea of the numbers, appreciate it! I should of asked for that initially.

In the meantime, you can try these possible combos:

local rayDirection = (Mouse - Tool.Handle.Position).unit * 1000 -- only change this line
local rayDirection = (Mouse - Tool.Handle.Position).unit * 1000 -- change this line and below one
local rayDesti = rayDirection + Tool.Handle.Position


Now this is very strange.

1 Like

Oh my god. This is why I hate working with raycasts LOL.

Add this to the code right after local raycast = … and just describe anything important you see, or any strange raycast behavior. If you feel like it, record it.

local beam = Instance.new("Beam")
beam.Parent = workspace
beam.Attachment0 = Instance.new("Attachment")
beam.Attachment0.Parent = Tool.Handle
beam.Attachment0.Position = Vector3.new(0, 0, 0)
beam.Attachment1 = Instance.new("Attachment")
beam.Attachment1.Parent = workspace
beam.Attachment1.Position = rayDesti

If there isnt anything strange, we can say for sure that the issue has to do with something we are not ecluding in the raycastparams


Yeah it wouldn’t let me parent the attachment1 to workspace so I just put it in to the SpawnLocation lol. Idk if this is useful.

1 Like

bruh you both put the wrong calculation for the direction
to find the direction you should do this:
(EndPoint.Position - StartPoint.Position).Unit * 100 (100 is the range)

local rayDirection = Mouse - Tool.Handle.Position
change this to

local rayDirection = (Mouse - player.Character.HumanoidRootPart.Position).Unit * 100

tell me if it works or not

2 Likes