Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

[Solved] Hitbox Raycast Problem?

Asked by
SnowieDev 171
3 years ago
Edited 3 years ago

I'm making a fighting game (another jojo game) and I'm having problems with making a hitbox using raycast. It's saying that the variable "CastResults" == nil

Extra Information: The variable "Stand" is a model, containing R6 Rigparts, it's primarypart is the HumanoidRootPart

My failed output:

ServerScriptService.StandServerHandler:233: attempt to index nil with 'Instance'

My raycast code:

local RaycastParameters = RaycastParams.new()
RaycastParameters.FilterType = Enum.RaycastFilterType.Blacklist
RaycastParameters.FilterDescendantsInstances = {Stand, Character}
RaycastParameters.IgnoreWater = true

local CastResults = Workspace:Raycast(Stand.PrimaryPart.CFrame.Position, Stand.PrimaryPart.CFrame.LookVector * 500, RaycastParameters)

local Hit = CastResults.Instance -- Line 233
local HitPosition = CastResults.Position
local Surface = CastResults.Normal
local Material = CastResults.Material.Name
0
You don't need [Unsolved] in your title. :) WideSteal321 773 — 3y
1
The mathematics used to draw the ray is likely the root of your issue. If your RaycastResult Object is nil, the ray didn't make contact with an Instance; it's best to account for this in the future. Ziffixture 6913 — 3y

1 answer

Log in to vote
1
Answered by
Necro_las 412 Moderation Voter
3 years ago
Edited 3 years ago

The raycast result not aways returns something, it can be nil if the ray did not find anything and ur code stops working after that.

Just check it out with

if CastResults then
    local Hit = CastResults.Instance
    local HitPosition = CastResults.Position
    local Surface = CastResults.Normal
    local Material = CastResults.Material.Name
end
Ad

Answer this question