Index nil with 'normal"

this is a script i use for my sliding with physics

local CAS = game:GetService("ContextActionService")
local UIS = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local tweenservice = game:GetService("TweenService")
local char = script.Parent

local slideAnim = Instance.new("Animation")
slideAnim.AnimationId = "rbxassetid://18657884998"

local shiftKey = Enum.KeyCode.LeftShift
local crouchKey = Enum.KeyCode.C
local acceleration = 35
local tweenedTransition = false

local hrp = char:WaitForChild("HumanoidRootPart")
local hum = char:WaitForChild("Humanoid")
local slidingAttachment = Instance.new("Attachment")
slidingAttachment.Name = "SlidingAttachment"
slidingAttachment.Parent = hrp

local alignOrientation = Instance.new("AlignOrientation")
alignOrientation.Enabled = false
alignOrientation.RigidityEnabled = not tweenedTransition
alignOrientation.Mode = Enum.OrientationAlignmentMode.OneAttachment
alignOrientation.Attachment0 = slidingAttachment
alignOrientation.Parent = hrp

local canslide = true
local sliding = false
local shiftPressed = false
local controlPressed = false
local crouchPressed = false

local leftPressed = false
local rightPressed = false

local playAnim = hum:LoadAnimation(slideAnim)
playAnim.Looped = true

local params = RaycastParams.new()
params.FilterDescendantsInstances = {char}
params.FilterType = Enum.RaycastFilterType.Exclude

local equippedTool = nil

local function stopSliding()
	if not sliding then return end
	sliding = false
	alignOrientation.Enabled = false
	hum.AutoRotate = true

	hum:ChangeState(Enum.HumanoidStateType.GettingUp)
	playAnim:Stop(0.5)

	if equippedTool then
		hum:EquipTool(equippedTool)
	end

	task.wait(0.5)
	canslide = true
end

local function slideFunc()
	if not canslide or hum.Health <= 0 or hum:GetState() ~= Enum.HumanoidStateType.Running or hum.WalkSpeed < 10 or hrp.Anchored then
		return
	end

	canslide = false
	sliding = true
	alignOrientation.Enabled = true
	hum:ChangeState(Enum.HumanoidStateType.Physics)

	hrp.AssemblyLinearVelocity = hrp.CFrame.lookVector * acceleration
	hum.AutoRotate = true

	equippedTool = nil
	for _, tool in ipairs(char:GetChildren()) do
		if tool:IsA("Tool") and tool.Parent == char then
			equippedTool = tool
			break
		end
	end

	hum:UnequipTools()
	playAnim:Play()
	script.Slide:Play()

	

	while sliding and hrp.Velocity.Magnitude > 0.1 do
		local speed = hrp.AssemblyLinearVelocity.Magnitude
		local intensity = speed / 100  

		task.wait()
	end

	stopSliding()
end

local function onInputBegan(input, gameProcessed)
	if input.KeyCode == shiftKey then
		shiftPressed = true
		if crouchPressed then
		end
	elseif input.KeyCode == crouchKey then
		controlPressed = true
		if shiftPressed and not gameProcessed then
			slideFunc()
		end
	elseif input.KeyCode == Enum.KeyCode.A then
		leftPressed = true
	elseif input.KeyCode == Enum.KeyCode.D then
		rightPressed = true
	end
end

local function onInputEnded(input, gameProcessed)
	if input.KeyCode == shiftKey then
		shiftPressed = false
		if sliding then
			stopSliding()
		end
	elseif input.KeyCode == crouchKey then
		controlPressed = false
		if sliding then
			stopSliding()
		end
	elseif input.KeyCode == Enum.KeyCode.A then
		leftPressed = false
	elseif input.KeyCode == Enum.KeyCode.D then
		rightPressed = false
	end
end

UIS.InputBegan:Connect(onInputBegan)
UIS.InputEnded:Connect(onInputEnded)

local tweenInfo = TweenInfo.new(0.1)

game:GetService("RunService").RenderStepped:Connect(function()
	if not sliding then return end

	local result = workspace:Raycast(hrp.Position, Vector3.new(0, -4.5, 0), params)
	local linearVelocity = hrp.AssemblyLinearVelocity

	if result then
		hrp.AssemblyLinearVelocity = linearVelocity - result.Normal * linearVelocity:Dot(result.Normal)
		alignOrientation.CFrame = CFrame.lookAt(Vector3.zero, hrp.AssemblyLinearVelocity)

		local currentRightVector = hrp.CFrame.RightVector
		local upVector = result.Normal
		local newFacialVector = currentRightVector:Cross(upVector)
		local tween = tweenservice:Create(alignOrientation, tweenInfo, {
			CFrame = CFrame.fromMatrix(hrp.Position, currentRightVector, upVector, newFacialVector)
		})
		tween:Play()
	else
		hrp.AssemblyLinearVelocity = Vector3.zero
	end

	local slideDirection = hrp.CFrame.lookVector
	--if leftPressed then
	--	slideDirection = slideDirection - hrp.CFrame.RightVector * slideControlFactor
	--elseif rightPressed then
	--	slideDirection = slideDirection + hrp.CFrame.RightVector * slideControlFactor
	--end

	slideDirection = (slideDirection - result.Normal * slideDirection:Dot(result.Normal)).Unit
	hrp.AssemblyLinearVelocity = slideDirection * hrp.AssemblyLinearVelocity.Magnitude
end)

however at the second to last line in the runservice section it gives me an error saying that it indexed nil with “normal”

slideDirection = (slideDirection - result.Normal * slideDirection:Dot(result.Normal)).Unit

any thoughts?

It’s saying that result doesn’t exist. As result is defined as the result of a raycast, it means that the ray failed to find a part within the parameters.

so is there anyway to get it to work? do i have to make the ray somehow bigger?

Previously in your code (line 146) you are checking that result actually exists, so you may just need to put the erroring code within the same if statement

but if im on a slope and im heading downwards its still giving me that error… do i have to change the raycast pos?

The error should not happen at all if you correctly place it within the if statemennt.

I managed to fix it by setting a new orientation to the raycast, but thank you anyway

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.