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!
What is the issue?
I want to keep face after death (or give random face again).
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)
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)
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
Edit: I moved the math.random() INTO the player so each time a player dies/rejoins, the face is different!