Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

[Solved] Hitbox Raycast Problem?

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

2 comments

0

You don't need [Unsolved] in your title. :)

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.

1 answer

✓ Accepted answer

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

This discussion is preserved as it was. New answers, comments, and votes are closed.