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
4 years ago
Edited 4 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:

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

My raycast code:

01local RaycastParameters = RaycastParams.new()
02RaycastParameters.FilterType = Enum.RaycastFilterType.Blacklist
03RaycastParameters.FilterDescendantsInstances = {Stand, Character}
04RaycastParameters.IgnoreWater = true
05 
06local CastResults = Workspace:Raycast(Stand.PrimaryPart.CFrame.Position, Stand.PrimaryPart.CFrame.LookVector * 500, RaycastParameters)
07 
08local Hit = CastResults.Instance -- Line 233
09local HitPosition = CastResults.Position
10local Surface = CastResults.Normal
11local Material = CastResults.Material.Name
0
You don't need [Unsolved] in your title. :) WideSteal321 773 — 4y
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 — 4y

1 answer

Log in to vote
1
Answered by
Necro_las 412 Moderation Voter
4 years ago
Edited 4 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

1if CastResults then
2    local Hit = CastResults.Instance
3    local HitPosition = CastResults.Position
4    local Surface = CastResults.Normal
5    local Material = CastResults.Material.Name
6end
Ad

Answer this question