Help with Raycasting on to the ground

I’m trying to rewrite the animation code and use procedural animation, but I first need to figure out how to even cast the attachments on the ground. When the game starts everything is in it’s place, but I get an error saying that the instances rightLeg and leftLeg don’t exist. On top of that, the forums are kind of confusing and I want to avoid a migraine.

Error message: Workspace.Lil_devboy123.Animate:30: attempt to index nil with 'FilterDescendantsInstances' - Client - Animate:30

Code:

local players = game:GetService("Players")
local player = players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
if not char then
	repeat wait() until char
	char = player.Character
end
local rightLeg = char:FindFirstChild("Right Leg") or char:WaitForChild("Right Leg")
local leftLeg = char:FindFirstChild("Left Leg") or char:WaitForChild("Left Leg")
local RS = game:GetService("RunService")
local Workspace = game:GetService("Workspace")








if leftLeg and rightLeg then
	local leftA = Instance.new("Attachment")
	local rightA = Instance.new("Attachment")
	leftA.Visible = true
	rightA.Visible = true
	leftA.Parent = leftLeg
	rightA.Parent = rightLeg
	RS.RenderStepped:Connect(function()
		local rayOrigin = rightLeg.Position
		local rayDirection = Vector3.new(0, -100, 0)
		raycastParams.FilterDescendantsInstances = {leftLeg, rightLeg}
		raycastParams.FilterType = Enum.RaycastFilterType.Exclude
		local raycastResult = Workspace:Raycast(rayOrigin, rayDirection, raycastParams)
		local raycastParams = RaycastParams.new()

		rightA.Position = rightLeg.Position + raycastResult.Distance
	end)
end
2 Likes

at first glance, it looks like you just forgot to make a variable. You are going to want to make a variable for recastparms using RaycastParams.new(), which, looking a few lines later, you did, so, you just need to move that variable 4 lines up.

        local rayDirection = Vector3.new(0, -100, 0)
        local raycastParams = RaycastParams.new()
		raycastParams.FilterDescendantsInstances = {leftLeg, rightLeg}
		raycastParams.FilterType = Enum.RaycastFilterType.Exclude
		local raycastResult = Workspace:Raycast(rayOrigin, rayDirection, raycastParams)
1 Like

I was pasting stuff around a lot, so I just made a little mistake. Thanks for the help!