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
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
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.
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.
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
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.
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.
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