You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Keep it simple and clear!
A raycast for a combat system that detects what is in front of the player
What is the issue? Include screenshots / videos if possible!
it ALWAYS generates the test part below or above the character, never in front
What solutions have you tried so far? Did you look for solutions on the Creator Hub?
A lot. Adjusted the Y value, changed the X and Z values to fixed values, I always have the problem
After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
local rs = game:GetService("ReplicatedStorage")
rs.DamageDealer.OnServerEvent:Connect(function(plr)
local Params = RaycastParams.new()
Params.FilterType = Enum.RaycastFilterType.Exclude
Params.FilterDescendantsInstances = {plr.Character:GetChildren()}
local origin = Vector3.new(0,0,0)
local direction = Vector3.new(0,0,0)
local raycast = workspace:Raycast(plr.Character:FindFirstChild("HumanoidRootPart").Position, Vector3.new(plr.Character:FindFirstChild("Head").Orientation.X, plr.Character:FindFirstChild("Head").Orientation.Y, plr.Character:FindFirstChild("Head").Orientation),Params)
print(raycast)
print(plr.Character:FindFirstChild("Head").Orientation.X)
local myPart = Instance.new("Part")
myPart.Parent = workspace
myPart.Position = raycast.Position
end)
Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.
Well, issue 1, you’re creating an origin and direction paramater and then just not using it.
origin should be the HumanoidRootPart’s position, and direction should be the LookVector * raycast distance of the HumanoidRootPart.
local rayDistance = 5 --// 5 studs
local origin = HumanoidRootPart.Position --// Position of the player
local direction = HumanoidRootPart.CFrame.LookVector * rayDistance --// Direction the player is looking * distance
local ray = workspace:Raycast(origin, direction, paramaters)
And then, issue 2:
If the raycast hits nothing, then it’ll return nil, thus, raycast.Position doesn’t exist.
myPart.Position = (ray and ray.Position or origin + direction)
And finally, issue 3, albeit not a big one, can be optimized more.
You can remove the :GetChildren() call on the character, as the filter is based off hierarchy. If for example you wanted to exclude ONLY the head, then you’d do Character:WaitForChild("Head"), but if you want the whole character, simply Character will suffice.