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

Random Face on Spawn Script Improvements?

This script works most of the times, but there are moments when it doesn't work on a specific player.

local ContentProvider = game:GetService("ContentProvider")
repeat wait() until game.Players.LocalPlayer.Character ~= nil
repeat wait() until (ContentProvider.RequestQueueSize == 0)

local Plr = game.Players.LocalPlayer
local Char = game.Players.LocalPlayer.Character

local ChangeFace = true
local FaceId = {574701104, 574701264, 574701296, 574701328, 574701359, 574701387, 574701414, 574701431, 574701479, 574701532}


wait(0.5)

if ChangeFace then
local var = FaceId[math.random(1,10)] -- Increase the '10' to amount of IDs you put in the 'FaceId'
Char:WaitForChild("Head").face.Texture = ("http://www.roblox.com/asset/?id=" .. var)
end

I've heard that it this script is dangerous and there could be more room for improvements.

Someone told me that I should look into PreLoading.

1 answer

Alright so you can easily change

local var = FaceId[math.random(1,10)] -- Increase the '10' to amount of IDs you put in the 'FaceId'

to be automatic by changing 10 to #FaceId, so FaceId[math.random(1,#FaceId)]

Also do not use "http://www.roblox.com/asset/?id=", just put "rbxassetid://"

By the way, games now a days should be using filtering enabled, so you should handle this on the server using Player.CharacterAdded

local FaceId = {574701104, 574701264, 574701296, 574701328, 574701359, 574701387, 574701414, 574701431, 574701479, 574701532}

function SpawnCharacter(Char)
	local var = FaceId[math.random(1,#FaceId)]
	Char:WaitForChild("Head"):WaitForChild("face").Texture = ("rbxassetid://" .. tostring(var))
end

game.PlayerAdded:connect(function(Player)
	Player.CharacterAdded:connect(SpawnCharacter)
	if Player.Character then SpawnCharacter(Player.Character) end
end)

This script will also work almost (if not every) time a player's character spawns

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