So I have a script that should save rocks under the enemy when he lands on the ground, but the problem is that my raycast shows “nil”. Here is the part of my script which is responsible for the raycast!
local function crater(data)
local params = RaycastParams.new()
params.FilterDescendantsInstances = {game.Workspace.Map}
params.FilterType = Enum.RaycastFilterType.Exclude
local currentTime = tick()
local craterRaycastResult
repeat
craterRaycastResult = workspace:Raycast(data.Target.PrimaryPart.Position, data.Direction, params)
wait()
until craterRaycastResult ~= nil or tick() - currentTime > 5
if craterRaycastResult then
for i = 0, 14 do
local part = Instance.new("Part", workspace.Fx)
part.Size = Vector3.new(4, math.random(10, 20)/10, math.random(10, 20)/10)
part.Anchored = true
part.CFrame = CFrame.new(craterRaycastResult.Position, craterRaycastResult.Position + craterRaycastResult.Normal)
part.CFrame = part.CFrame * CFrame.Angles(math.rad(90), math.rad(i * 360/14), 0) * CFrame.new(0, 0, -4 * 2) * CFrame.Angles(math.rad(35), 0, 0)
part.CanQuery = false
part.CanCollide = false
part.CanTouch = false
print(params)
local result = workspace:Raycast(part.Position + craterRaycastResult.Normal * 4, craterRaycastResult.Normal * -5, params)
--local result = workspace:Raycast(part.Position + craterRaycastResult.Normal * math.rad(40), craterRaycastResult.Normal * math.rad(-50), params) -- BUG
print(result) -- nil
if result then
part.Position = result.Position
part.Material = result.Material
part.Color = result.Instance.Color
else
part:Destroy()
end
From what I see in the script, the “params” variable is defined with a FilterDescendantsInstances property equal to {game.Workspace.Map}, which means that only instances that are children of the game.Workspace.Map instance will be hit by the raycast.
Make sure that the object you are trying to hit is indeed a child of the game.Workspace.Map, otherwise, the raycast won’t hit it.
You could try removing the FilterDescendantsInstances property from the params variable, or change it to include more objects in the raycast.
If you need I can give you the game file, I’m testing their combat system so there is nothing but her. Here is the file: King Piece, Test.rbxl (58.3 KB)