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

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

Asked by
Abhorra 16
3 years ago

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?

0
plr is nil. raid6n 2196 — 3y
0
Yeah umh, and what should I do about it? No idea how to set "plr" to an actual player. Abhorra 16 — 3y
0
MouseButton1Click doesn't have a parameter for the player Pupppy44 671 — 3y
0
Hey you why don't you accept the person's answer if he helped you Shounak123 461 — 3y
0
I completely forgot to, my bad! Accepted. Abhorra 16 — 3y

2 answers

Log in to vote
2
Answered by 3 years ago

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)

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. Abhorra 16 — 3y
0
Alright! I'm glad I've helped, but can you please accept my answer? Thank you. Gabe_elvin1226aclan 323 — 3y
0
Hey Gabe, sorry! I completely forgot to, I just did it. Apologies for taking so long. Abhorra 16 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

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

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? Abhorra 16 — 3y

Answer this question