Unable to get raycast to detect if play is standing under something

I am trying to determine if a player is standing under something.
I am using raycast with target set 10 above the Y of the players head.
I am getting Handle as the name for the raycast results instance name.
I have a test place which I have attached here.
It has two objects in the workspace that the player can stand under.

TestPlace5.rbxl (84.5 KB)

Anyone any ideas for me.

1 Like

This isn’t all too difficult to implement. I wrote a script for you that’s intended to operate through a LocalScript in StarterPlayerScripts:

--!strict
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")


local RAY_DIRECTION = Vector3.yAxis * 100


local Player = Players.LocalPlayer


local raycastParameters: RaycastParams
local raycastConnection: RBXScriptConnection



local function tryStopRaycasting()
	if raycastConnection then
		raycastConnection:Disconnect()
	end
end

local function onCharacterAdded(character: Model)
	tryStopRaycasting()
	
	local humanoid = character:FindFirstChildOfClass("Humanoid") :: Humanoid
	if humanoid then
		humanoid.Died:Once(tryStopRaycasting)
	end
	
	raycastParameters = RaycastParams.new()
	raycastParameters.FilterDescendantsInstances = {character}
	raycastParameters.FilterType = Enum.RaycastFilterType.Exclude
	
	raycastConnection = RunService.PostSimulation:Connect(function()
		local origin = character:GetPivot().Position
		local result = workspace:Raycast(origin, RAY_DIRECTION, raycastParameters)
		
		if not result then
			return
		end
		
		print(`{Player} is underneath {result.Instance:GetFullName()}`)
	end)
end



local character = Player.Character

if character then
	onCharacterAdded(character)
end

Player.CharacterAdded:Connect(onCharacterAdded)
Player.CharacterRemoving:Connect(tryStopRaycasting)

Thank you. I can see that would work but in my case it has to work on the server.
I am doing it on the client and the server to confirm they are the same.
I tried using FloorMaterial and the results were different.
I tried doing raycast downwards from RightFoot and got no results.
The player has to stand inside a kiosk that has a roof if its not it dies.
I was havng a problem where the hit was Handle but that was because the player had a hat on.
For this test phase I am not allowing hats.
I am continuing to try to fix my scripts

Sorted out my problem. The Handle is part of a hat.
My avatar was wearing one.
I removed that and the raycasting is working return nil if no roof or the part name if there is one.

Translating that code to work on the server requires little to no effort—the logic largely remains the same

Thanks for that insight. I have succeeded in gettting it to work on server and client but they do not agree with eachother.
As I need the status from all clients I am now looking at using FireAllClients and get the client to return what it sees.

How do they disagree? The server’s opinion is all that matters if you’re trying to reach a consensus with the server’s players. If the opinion is truly far off, then you most likely have an implementation problem

RaycastingScripts.rbxm (2.8 KB)

This contains both client and server implementations

I agree with you that there might be an implementation issue.
The scenario is that there is a place event which effects all players so in the server script I freeze all the players then use allplayers to get each player and check if that player is in a safe place (under something) and if so leave it alone otherwise strip off all items that the player has collected since the last scene reset (happens when scene reaches a contents limit) or last place event.
I am getting different values when that check is on the server where it print the status out and the client which prints the status out every second when the player walkspeed is 0.
I can verify the player is under something by viewing the game but cannot verify the same way on the server hence using prints.