Getting `FloorMaterial`

Hi guys,

Basically I only want to find the floor material; or the material that the player is walking on.

It is simply enough to do humanoid.FloorMaterial, but this cannot work in my case.

I am using Gravity Controller which has the player slightly off of the ground; and on top of that it keeps invisible parts under the player in order to control gravity more easily.

That said I believe I have a way around these roadblocks; however I am not sure if it is possible or what the best way would be to go about this, which is why I am making this post.

Raycasting may work, as I have already used it in my game before; however, I am sure a constant raycast happening would be strain on resources. Either server or client.

I would prefer another option where I could return the material a stud or 2 under the Character’s feet. I believe I am seen this before but cannot remember how it worked or how it was coded.

I believe it was a part below the character and it used Touched to return the information that was needed, in order for more to code to run.

If anyone can get me moving in the right direction, please feel free to give me some insight!

Short raycasts of 1-2 studs aren’t a strain at all, I think raycasting would be easier here.

2 Likes

I think raycasting would be the appropriate thing to use here.

1 Like

Okay then, I would probably want to run this off of RunService then right?

Or is there an alternative that would be better?

@SanctuaryIX

It depends on your use case. If you really need it often then RunService would be fine. I believe there wouldn’t be that much strain. If you only need it when something happens you might want to look at an event.

1 Like
local TouchPart = Instance.new("Part", workspace)
TouchPart.Anchored = true
TouchPart.CanCollide = false
TouchPart.Size = Vector3.new(1, 1, 1)
TouchPart.Transparency = 1
local TouchConnection = TouchPart.Touched:Connect(function() end)


function GetPartsBelowPlayer(Player)
	local Character = Player.Character
	if Character then
		local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
		if HumanoidRootPart then
			TouchPart.CFrame = HumanoidRootPart.CFrame + Vector3.new(0, -3.5, 0)
			return TouchPart:GetTouchingParts()
		end
	end
end
1 Like

So i’ve looked at the Humanoid.FloorMaterial property on the official site and it says

NotReplicated
This item is not replicated across Roblox’s server/client boundary.

which means it may have a different output depending on the if it is the server or the client so try a print on both and see the result

Incase you want to see the page for yourself: Humanoid.FloorMaterial

There’s an issue with this, it casts straight down below the player at all times, not to its orientation. Usually you want to subtract the UpVector times the distance expected.

Raycasting does not cost a lot of memory in usage, it’s unbelievably low-cost and accurate method. Use an appropriate loop for it and it will function seamlessly.

3 Likes

Yeah I thought that would be the case but I left it to be fixed by the post creator for his needs

1 Like
local function GetPartBeneathPlayer(Player, BlacklistArray, Distance)
	local Character = Player.Character
	if Character then
		local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
		if BlacklistArray == nil then BlacklistArray = {} end
		if Distance == nil then Distance = 5 else Distance = math.abs(Distance) end
		table.insert(BlacklistArray, Character)
		local RayParameters = RaycastParams.new()
		RayParameters.FilterDescendantsInstances = BlacklistArray
		RayParameters.FilterType = Enum.RaycastFilterType.Blacklist
		RayParameters.IgnoreWater = true
		local RayOrigin = HumanoidRootPart.Position
		local RayDestination = HumanoidRootPart.CFrame + (HumanoidRootPart.CFrame.UpVector * -Distance)
		local RayDirection = RayDestination - RayOrigin
		local RaycastResult = workspace:Raycast(RayOrigin, RayDirection.Position, RayParameters)
		if RaycastResult ~= nil then
			if RaycastResult.Instance ~= nil then
				return RaycastResult.Instance
			end
		end
	end
end
1 Like

i dont think raycasting is very efficient for this. use a touched event on the foot and then do object touched.material