How do I detect the color of the part player is standing on?

Just like Humanoid.Floor.Material, I need to get the color of the floor too. How would I do that?

5 Likes

I believe you can’t get the part the player is standing on this way.
However, you can used the Humanoid’s Touched event which detects what part has touched the Humanoid’s limb. This may not be exactly efficient because it fires constantly when the player is moving instead at a given time.

This is a rough example for R6 characters:

Player.Character.Humanoid.Touched:Connect(function(PartHit, LimbPart)
	if LimbPart == "Left Leg" or "Right Leg" then
		-- Do whatever you want to do
	end
end)
1 Like

Raycasting.

You can cast a ray downwards from the humanoidrootpart and get a part’s color.

I can write up a demonstration script when I get home for you.

4 Likes

More precisely, downcasting. Don’t worry, I got you. I think.

local Ray = Ray.new(RootPart.Position, Vector3.new(0, -3, 0))
local FloorPart = workspace:FindPartOnRay(Ray, IgnoreDescendantsObject)

For a reference on how I organised the variables, see Ray datatype documentation. Essentially:

  • First argument takes the origin, which I set to the RootPart’s position
  • Second argument takes a directional vector, which I made 3 studs down
  • Construct a ray using the above arguments, then pass it into a raycast method

(Happy anniversary on the DevForum)

30 Likes

The IgnoreDescendantsObject shows warning, what’s wrong with it?

4 Likes

You’re not supposed to type that out explicitly, you’re supposed to set an object. Like for example, you could replace it with “Character” (a variable that references the Character model).

2 Likes

You can put this in a local script in StarterPlayerScripts

local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local HumanoidRP = Character:WaitForChild("HumanoidRootPart")

while wait(1) do
    local newRay = Ray.new(HumanoidRP.CFrame.p, Vector3.new(0,-1,0).unit * 10)
    local Hit = game:GetService("Workspace"):FindPartOnRay(newRay, Character)

    if Hit then
        local HitBrickColor = Hit.BrickColor
        local HitColor3 = Hit.Color
        --print(HitBrickColor)
        --print(HitColor3)
    end
end
11 Likes

Hey! That worked but it keeps finding the legs of the character, how do I ignore the descendants of the character?

2 Likes

Use Raycast Ignorelist

local hit, position = workspace:FindPartOnRayWithIgnoreList(ray, ignoreList)

You could try this:

local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local HumanoidRP = Character:WaitForChild("HumanoidRootPart")
local IgnoreList = {}
table.insert(IgnoreList, Character)

while wait(1) do
    local newRay = Ray.new(HumanoidRP.CFrame.p, Vector3.new(0,-1,0).unit * 10)
    local Hit, position = game:GetService("Workspace"):FindPartOnRayWithIgnoreList(newRay, IgnoreList)

    if Hit then
        local HitBrickColor = Hit.BrickColor
        local HitColor3 = Hit.Color
        --print(HitBrickColor)
        --print(HitColor3)
    end
end

Even though :FindPartOnRay has an ignore parameter, it only allows one Instance to be ignored, not an array. This is why you’d use :FindPartOnRayWithIgnoreList

9 Likes

Actually it ignores that instance and all its descendants, so it shouldn’t interfere with the legs. (I’ve also tested my code before posting it here and it was working just fine.)

4 Likes