Index nil with 'Normal'

Hello. I’m having problems with my raycast code. It’s returning me the index nil with ‘Normal’ error and I’m confused on why it’s giving this error to me. I’m not the best best with raycast so this could help me if it can be solved. Thank You.

– Function

local function BounceDirection(BallVelo)
	local LinearPush = Football:FindFirstChild('LinearPush')
	-- Check --
	if not LinearPush then
		return nil,false
	end
	-- Cast Params --
	local CastParams = RaycastParams.new()
	CastParams.FilterType = Enum.RaycastFilterType.Include
	CastParams.FilterDescendantsInstances = {workspace.Baseplate}
	-- Linear Settings --
	local Unit = LinearPush.VectorVelocity.Unit
	local LinearResult = game.Workspace:Raycast(Football.CFrame.Position - Unit * 10,Unit * 10 * 2,CastParams)
	-- Check --
	if LinearResult == nil or not LinearResult.Instance or not (LinearResult.Instance.Transparency < 1) then
		print('Bounce')
	end
	---
	return (Unit - 2 * Unit:Dot(LinearResult.Normal) * LinearResult.Normal) * LinearPush.VectorVelocity.Magnitude
end

– Error Line

return (Unit - 2 * Unit:Dot(LinearResult.Normal) * LinearResult.Normal) * LinearPush.VectorVelocity.Magnitude

1 Like

The “attempt to index nil with (Value)” error occurs because you’re attempting to access a value “Normal” that returns a nil value.

1 Like

LinearResult is nil. You have to stop the code in this if statement

if LinearResult == nil or not LinearResult.Instance or not (LinearResult.Instance.Transparency < 1) then
	print('Bounce')
    return --something else, or just stop the code here
end
3 Likes

Is that why my football does this when it touches the ground?

1 Like

Of course that was suppose to happen when interacting with nil value

1 Like

I made it so it prints things threwout the script. How could I make it so it isn’t “nil” anymore and it actually is something.

local function BounceDirection(BallVelo)
	local LinearPush = Football:FindFirstChild('LinearPush')
	-- Check x2 --
	if not LinearPush then
		if BallVelo then
			print('No Velo')
		end
		return nil,false
	end
	-- Cast Params --
	local CastParams = RaycastParams.new()
	CastParams.FilterType = Enum.RaycastFilterType.Include
	CastParams.FilterDescendantsInstances = {workspace.Baseplate}
	-- Linear Settings --
	local Unit = LinearPush.VectorVelocity.Unit
	local LinearResult = game.Workspace:Raycast(Football.CFrame.Position - Unit * 10,Unit * 10 * 2,CastParams)
	-- Check --
	if LinearResult == nil or not LinearResult.Instance or not (LinearResult.Instance.Transparency < 1) then
		-- Check --
		if BallVelo then
			print('No Reaction')
		end
		return nil,false
	end
	-- Check --
	if BallVelo then
		print(LinearResult.Normal)
		print('Bounce')
	end
	---
	return (Unit - 2 * Unit:Dot(LinearResult.Normal) * LinearResult.Normal) * LinearPush.VectorVelocity.Magnitude
end
1 Like