Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Unable to cast value to Object?

I want to identify a humanoid with a right leg, and change the brickcolor to Really red. I keep getting this error and I cannot figure out what's wrong. Thanks!

part = script.Parent.Part
game.Players.PlayerAdded:connect(function()
	local plr = {game.Players}
	for i,v in pairs(plr) do
		char = v:GetPlayerFromCharacter(plr)
		if char then
			hum = char:FindFirstChild("Humanoid") 
			if hum then
				hum.RightLeg.BrickColor = BrickColor.new("Really red")
			end
		end
		
	end
end)

2 answers

✓ Accepted answer

Notice on line 7 you define hum as a Humanoid. On line 9, the hierarchy is incorrect. You are basically saying that Humanoid.RightLeg.BrickColor = BrickColor.new("Really red"), which will error because Humanoid does not contain a RightLeg. This should fix the problem:

part = script.Parent.Part
game.Players.PlayerAdded:connect(function()
    local plr = {game.Players}
    for i,v in pairs(plr) do
        char = v:GetPlayerFromCharacter(plr)
        if char then
            hum = char:FindFirstChild("Humanoid") 
            if hum then
                hum.Parent.RightLeg.BrickColor = BrickColor.new("Really red") --referring to Humanoid's parent, the Player
            end
        end

    end
end)

I hope this helps!

2 comments

0

Helpful, however now I'm still getting an error that says "Unable to cast value to object" on Line 5.

0

Try char = game.Players:GetPlayerFromCharacter(v).

Hey, I read your script and I wasn't exactly sure what you wanted to do 100% so I gave you two examples.

part = script.Parent.Part

-- if you want the joining player ONLY to have that right leg red
game.Players.PlayerAdded:connect(function(plr) -- When the player joins it is assigned to the variable plr
	repeat wait() until plr.Character -- waits for the player's character to load to avoid errors

	if plr.Character:FindFirstChild("Humanoid") then -- checks if the player has a humanoid if so then do :
		plr.Character.RightLeg.BrickColor = BrickColor.new("Really red"); -- makes the player's Right Leg Really red.
	end
end)

-- if you want all the players to have red leg

game.Players.PlayerAdded:connect(function()
	for _, player in pairs(game.Players:GetPlayers()) do -- gets the list of all players in the game once a player joins
		if player.Character:FindFirstChild('Humanoid') then -- checks if each player has a humanoid
			player.Character.RightLeg.BrickColor = BrickColor.new("Really red"); -- makes all players that have a humanoid have a Really red Right Leg
		end
	end
end)

4 comments

0

1 year late

0

This comment was written by the 5 year late gang

0

this comment was written by the 6 year late gang

0

this comment was written by the 7 year late gang

This discussion is preserved as it was. New answers, comments, and votes are closed.