I have a script in server script storage that's supposed to give each player a certain face in the game once the race value is returned.

The script is also located in ServerScriptService:

local Players = game:GetService("Players")



--List of names.
local species = {
	{Name = "Vampire"},
	{Name = "Human"},
	{Name = "Werewolf"}
}
--Should I put this in a module script? What good would that do?^

local resultingTable = {
	{Race = species[2], Weight = 100}, 
	{Race = species[3], Weight = 49},
	{Race = species[1], Weight = 1}
}
--Weights for each name have to be in greatest to least (I think, I don't know that might only apply to C# coding).

--Generates random number.
local function returnSumOfWeight(resultingTable)
	local sum = 0
	for _, entry in ipairs(resultingTable) do --For each item in the resultingTable^ 
		sum = sum + entry.Weight -- Basically, sum+=wight (total of the weights)
	end
	return sum --Ayy, totally tubular.
end

--Returns random name from the table.
local function getRandomRace(resultingTable)
	local randomNumber = math.random(returnSumOfWeight(resultingTable)) -- Returns a random number based on the total weight of the table ^
	
	for _, entry in ipairs(resultingTable) do --For each this in the resultingTable
		if  randomNumber <= entry.Weight then --If the random number is less than or equal to the the wieght of that name
			return entry.Race --Returns the name (the name is the result).
		else
			randomNumber = randomNumber - entry.Weight --If it is greater than the weight, subtract the weight from the random number to get a lower random number and continues to loop through.
		end
	end
end

--[[
--Tests the output of the get random item, and prints it 100 times.

for i = 1, 100 do
	local race = getRandomItem(resultingTable)
	print(race.Name)
end

--]]

--Moving on, to getting the race and changing the face (dang, rhymes for years).

function PlayerAdded(player)
	print(player.Name .. " has joined the server!")
	local function CharacterAdded(character)
		repeat wait() until player.character
		local race = getRandomRace(resultingTable)
		if race.Name == "Vampire" then
			character.Head.face.Texture = "rbxassetid://162638839"
		else if race.Name == "Human" then
			character.Head.face.Texture = "rbxassetid://3735688923"
		else if race.Name == "Werewolf" then
			character.Head.face.Texture = "rbxassetid://1117061264"
		end
		end
		end
	end
	CharacterAdded(player.character or player.CharacterAdded:Wait())
	player.CharacterAdded:Connect(CharacterAdded)
end


game.Players.PlayerAdded:Connect(PlayerAdded)
for _,player in pairs(game.Players:GetPlayers()) do
	spawn(function() PlayerAdded(player) end)
end

It works when I run it in studio, giving me the according face. However, when I tried TeamTesting it or even accessing it on Roblox it doesn't work.

Does anyone know how I can fix this?

2 comments

0

do you have Archivable to true?

0

Yes