Attempting to find a face with a variable from a table randomly based off of a character variable

Basically the character spawns in a personality and that personality determines what type of face they will spawn with, I’ve been trying for awhile to figure out how I can match the variable from the face I’ve put in a folder of faces with the personality the player has.

Some faces have multiple types that could align with the character personality which is why they have multiple string values.

Nothing seems to work! Any tips?

    local Personality = playerFolder:FindFirstChild("Personality").Value
	
	local faceOptions = game.ServerStorage.NPCAssets.Faces:GetChildren()
	
	local chosenface = faceOptions:FindFirstChild("Type") == Personality
	
	local randomface = (chosenface[math.random(#faceOptions)])
		
	if playerAppearance then
		for _,v in pairs(playerAppearance:children()) do
			if v:IsA("DataModelMesh") then
				Head.Mesh:Destroy()
				v.Parent = Head
				newHeadMesh = true	
				
			elseif v:IsA("Decal") then
				Head.face:Destroy()
				
				v.Name = "face"
				v.Parent = Character.Head
				v.Texture = randomface.Texture
				newFace = true

image
image


Tried to try this another way but it still didn’t work! Just got an error.

    local Personality = playerFolder:FindFirstChild("Personality").Value
	
	local faceOptions = game.ServerStorage.NPCAssets.Faces:GetChildren()
	
	local randomface = (faceOptions[math.random(#faceOptions)])
	
	local chosenface = randomface:FindFirstChild("Type").Value == Personality

time for explaining:

  • chosenface is a boolean, because there is a == operator.
  • randomface is indexing the boolean, which is chosenface with [math.random], which appearantly was all explained in the error.
    you cannot do: true["Texture"] or false["Texture"], because they are booleans that were made for performing logic

In which case, how would I make it so it would be able to “check” if it’s the correct value for the personality without making it repeat the randomization process over and over until it finds the correct one? Because when I tried that it made the script time-out.

can you give me your entire script, so i can understand what are you doing in it

I fixed it by reworking the way I did it, instead of searching for tags I opted for much simpler solution. I put each one in a folder and made the script search for the name of it based off of the personality!
image

	local Personality = playerFolder:FindFirstChild("Personality").Value
	
	local faceFolders = game.ServerStorage.NPCAssets.Faces
	
	local chosenFolder
	for i,v in pairs(faceFolders:GetChildren()) do
		if v.Name == Personality then
			chosenFolder = v:GetChildren()
		end
	end
	
	local chosenface = (chosenFolder[math.random(#chosenFolder)])

	if playerAppearance then
		for _,v in pairs(playerAppearance:children()) do
			if v:IsA("DataModelMesh") then
				Head.Mesh:Destroy()
				v.Parent = Head
				newHeadMesh = true	
				
			elseif v:IsA("Decal") then
				Head.face:Destroy()
				
				v.Name = "face"
				v.Parent = Character.Head
				--v.Texture = "rbxassetid://"..(faceOptions[math.random(#faceOptions)])
				v.Texture = chosenface.Texture
				newFace = true

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.