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

Face Changing GUI isn't working? - attempt to index nil with 'Character' error

Hello, I need some help. I'm trying to make a face changing GUI where, when the player clicks the Image Button in the GUI, their avatar's face will change to match the image they clicked.

This is what I got so far:


function applyFace(plr)
	    local h = plr.Character:findFirstChild("Head")
        if h~=nil then
        h.face.Texture = script.Parent.Image
    end
end

script.Parent.MouseButton1Click:Connect(applyFace)

For some reason, when I click on the face button, the output windows says "attempt to index nil with Character". Any idea on how to fix it?

5 comments

0

plr is nil.

0

Yeah umh, and what should I do about it? No idea how to set "plr" to an actual player.

0

MouseButton1Click doesn't have a parameter for the player

0

Hey you why don't you accept the person's answer if he helped you

0

I completely forgot to, my bad! Accepted.

2 answers

✓ Accepted answer

I think it's due to the parameter plr is nil. Just make a variable of player. Here's the script:

function applyFace()
    local plr = game.Players.LocalPlayer
    local h = plr.Character:WaitForChild("Head")
    if h ~= nil then
        h.face.Texture = script.Parent.Image
    end
end

script.Parent.MouseButton1Click:Connect(applyFace)

3 comments

0

Works perfectly, thank you so much! I thought by having the "plr" parameter in the function itself as applyFace(plr) the info would be passed thru the MouseButton1Click:Connect event lol.

0

Alright! I'm glad I've helped, but can you please accept my answer? Thank you.

0

Hey Gabe, sorry! I completely forgot to, I just did it. Apologies for taking so long.

Make sure to add on line 2 or plr.CharacterAdded:Wait()

Here is an example

function applyFace(plr)
        local h = plr.Character:findFirstChild("Head") or plr.CharacterAdded:Wait()
        if h~=nil then
        h.face.Texture = script.Parent.Image
    end
end

script.Parent.MouseButton1Click:Connect(applyFace)

Also check if you put your script as a local script in a StarterGui

1 comment

0

The script was already a local script & I added what you suggested, but sadly it still gives the same error: 14:07:04.880 - Players.Abhorra.PlayerGui.FaceChanger.ScrollingFrame.Folder.Face1.LocalScript:2: attempt to index nil with 'Character'. Anything else I could do?

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