Help with Raycasting from the HumanoidrootPart to the ground

Hello everyone!

I am trying to make a system where a script will raycast from a player’s HumanoidRootPart to the ground, and if the part fills a certain criteria it will set values inside of the part accordingly, and then when stepping off the part or if it doesn’t match the part with the values it sets them to nil/false

My problem though is I cant seem to get my raycasts to actually cast rays.

local runService = game:GetService("RunService")

local rayDirection = Vector3.new(0,-5,0)

local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {script.Parent}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

local memory = nil

local plr = game.Players:GetPlayerFromCharacter(script.Parent)

local function handleCast(part,pos,material,norm)
	print("I HIT SOMETHING!")
	if part:FindFirstAncestorWhichIsA"Folder" then
		part.Player.Value = plr
		part.Occupied.Value = true
		memory = part
	end
	if part ~= memory then
		memory.Occupied.Value = false
		memory.Player.Value = nil
	end
end

runService.PreSimulation:Connect(function()
	local rayOrigin = script.Parent.HumanoidRootPart.Position
	local rayDirection = Vector3.new(script.Parent.HumanoidRootPart.Position.X,script.Parent.HumanoidRootPart.Position.Y-5,script.Parent.HumanoidRootPart.Position.Z)
	local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)
	
	if raycastResult then
		handleCast()
	end
end)

Here is my script, I have no clue why its doing this. Any help is appreciated!

2 Likes

Your given direction is wrong. Simply change the direction to this:

local rayDirection = Vector3.new(0, -5, 0)

So the result position of the ray will be:

-- Result Position = origin + direction

I hope this helps! :slight_smile:

It still isn’t working properly, it isn’t even outputing anything despite the print statement

Hello, maybe the problem is that your function uses four parameters, but you aren’t using or filling the parameters so it doesn’t know what part, pos, norm, etc. are.

2 Likes

I am not 100% sure myself, but maybe it’s because these events weren’t enabled yet. Although it’s deprecated, using your code and replacing .PreSimulation with .Stepped worked.

2 Likes