Character Randomizer

This works! But the only problem I have is that the characters are spawning somewhere else instead of the spawn locations I have placed in my game. Any reason why?

I’m not entirely sure but it might be because it’s placing you where the character is at in world. When a base part is outside of workspace, it’s position doesn’t change, but the base part isn’t rendered and that’s probably why you’re spawning elsewhere. This can fixed with an easy line.

-- In a script in ServerScriptService

local characters = game.ServerStorage:WaitForChild("StarterCharacters"):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.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)

If you have multiple spawn locations in your map, then put them in a folder and replace the added line of code with this code:

local PlayerSpawns = workspace:WaitForChild("PlayerSpawns"):GetChildren() -- folder with spawn points in them
player.Character:WaitForChild("HumanoidRootPart").CFrame = PlayerSpawns[math.random(1, #PlayerSpawns)].CFrame

This works! Thank you so much man! Hope you have a good one!

2 Likes

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