Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Unable to cast value to Object?

Asked by
bloxxyz 274 Moderation Voter
9 years ago

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

Log in to vote
1
Answered by 9 years ago

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!

0
Helpful, however now I'm still getting an error that says "Unable to cast value to object" on Line 5. bloxxyz 274 — 9y
0
Try char = game.Players:GetPlayerFromCharacter(v). IcyArticunoX 355 — 9y
Ad
Log in to vote
1
Answered by
DevArk 50
8 years ago

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)
0
1 year late LoganboyInCO 150 — 5y
0
This comment was written by the 5 year late gang mixgingengerina10 223 — 4y
0
this comment was written by the 6 year late gang Omq_ItzJasmin 666 — 3y
0
this comment was written by the 7 year late gang Spookdoggz 42 — 2y

Answer this question