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

My Face giver script is not working at all and I dont know why?

Asked by 5 years ago

After a little help from a member on scripthelpers, I was able to get a script down that gives you things depending on a value, and I have this script right here that should give you a face if you have a value, but it doesn't, no output errors or anything, any help?

local bunchOfFaces = {
    [1] = "rbxassetid://3224406211", --Face1
    [2] = "rbxassetid://3224406505", --Face2
    [3] = "rbxassetid://3224406817", --Face3
}

game.Players.PlayerAdded:connect(function(player)
    player.CharacterAdded:Connect(function(character)       
        local d = character:WaitForChild("Head").face
        local val = player:WaitForChild("leaderstats").Face.Value
        if val >= 1 then
            wait(1)
            if val >= #bunchOfFaces then
                d.Texture =  bunchOfFaces[#bunchOfFaces]
            else
                d.Texture =  bunchOfFaces[val]
            end
        end
    end)
end)

1 answer

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

When a child of an Instance is loaded, the children of it may not load immediately, especially ones that took time to load like a player's character. Therefore, using local d = character:WaitForChild("Head").face may not be the most efficient way to do.

To solve this, set the local variable d to only wait for the charater's Head instead of indexing its face directly by the dot operator. (local d = character:WaitForChild("Head")). Since you yielded the code for 1 second, you can edit line 14 and line 16 to:

d.face.Texture = bunchOfFaces[#bunchOfFaces] --line 14
d.face.Texture = bunchOfFaces[val] --line 16

--A second is enough time for the decal to be added after the head was replicated.

You can also wait for the face to load, too!

--This is just a reference.
local f = d:WaitForChild("face")
f.Texture = bunchOfFaces[val]

Cheers! c:

0
I am confused Pooglies 7 — 5y
0
lol just replace where you assign the local d to character:WaitForChild("Head"), then change d.Texture to d.face.Texture Afterl1ght 321 — 5y
0
I see now, thank you so much, your so helpful! Pooglies 7 — 5y
Ad

Answer this question