I am making a Character Randomizer system where everytimme a new player starts playing they get one of selected characters and I have some code but it doesnt work, can anyone help? there is a screenshot and code below
-- In a script in ServerScriptService
local characters = game.ServerStorage:WaitForChild("Characters"):GetChildren() -- replace with where the folder is and the name of the folder
game.Players.PlayerAdded:Connect(function(player)
if #characters > 0 then
local function assignCharacter()
local starterChars = {}
for i, char in ipairs(characters) do
table.insert(starterChars, char)
end
local function randomCharacter()
local random = Random.new()
return starterChars[random:NextInteger(1, #characters)]
end
local newChar = randomCharacter():Clone()
player.Character = newChar
newChar.Parent = workspace
player.Character:WaitForChild("HumanoidRootPart").CFrame = workspace.Map.SpawnLocation.CFrame -- Line Added. Replace SpawnLocation with the name of the spawn point
end
player.CharacterAdded:Connect(function(c)
local h = c:WaitForChild("Humanoid")
h.Died:Connect(function()
task.wait(3)
assignCharacter()
end)
end)
assignCharacter()
else
print("There are no starter characters in StarterPlayer.")
player:LoadCharacter()
end
end)