Face Randomizer

  1. What do you want to achieve?
    Im beginner scripter, and i made a script that gives to a player random face, but i’ve got a problem!

  2. What is the issue?
    I want to keep face after death (or give random face again).

  3. What solutions have you tried so far?
    I looked for solutions on DevForum and Youtube.

Script:

local clients = game:GetService("Players")
local serverStorage = game:GetService("ServerStorage")
local faceFolder = serverStorage:FindFirstChild("Faces")
local faces = faceFolder:GetChildren()
local randomFace = faces[math.random(1,#faces)]

clients.PlayerAdded:Connect(function(client)
	wait()
	local char = client.Character or client.CharacterAdded:Wait()
	local Head = char:WaitForChild("Head")
	randomFace.Parent = Head
	print(randomFace)
end)

faces

1 Like

So basically to give them the same face when they die you need to use a Humanoid.Died event, here is some untested code that might work in your favor.


local clients = game:GetService("Players")
local serverStorage = game:GetService("ServerStorage")
local faceFolder = serverStorage:FindFirstChild("Faces")
local faces = faceFolder:GetChildren()
local randomFace = faces[math.random(1,#faces)]

clients.PlayerAdded:Connect(function(client)
	wait()
	local char = client.Character or client.CharacterAdded:Wait()
    local Humanoid = char:WaitForChild("Humanoid")
	local Head = char:WaitForChild("Head")
	randomFace.Parent = Head
	print(randomFace)

    Humanoid.Died:Connect(function()
        wait(1.5)
        local Head = char:WaitForChild("Head")
	    randomFace.Parent = Head
    end)
end)
1 Like

You’re able to achieve this with CharacterAdded

local clients = game:GetService("Players")
local serverStorage = game:GetService("ServerStorage")
local faceFolder = serverStorage:FindFirstChild("Faces")
local faces = faceFolder:GetChildren()


-- Getting our player
clients.PlayerAdded:Connect(function(client)
    -- Waiting for the character to (re)spawn
    client.CharacterAdded:Connect(function(character)
        -- Give them the face
        local randomFace = faces[math.random(1,#faces)]
        local Head = character:WaitForChild("Head")
        randomFace.Parent = Head
        print(randomFace)
    end)
end)

This will work because
Screen Shot 2021-07-24 at 7.34.07 AM

Edit: I moved the math.random() INTO the player so each time a player dies/rejoins, the face is different!

2 Likes

It works, but after the player respawn he doesn’t have face.

1 Like

Thank you!
It worked
Thank you both for help!

2 Likes