random StarterCharacter | Roblox Forum
Welcome to the Forums!
Join thousands of other Roblox members with a free account.
Create Forum Account

random StarterCharacter

sandro480

Member
Jun 25, 2020
37
5
8
Roblox
sandro480
i need help with making the StarterCharacters random. something like placing the characters in a folder and a script that chooses whitch one to be the character randomly every time someone respawns or joins.
 

CallMeKY

Moderator
Ultra
Feb 25, 2018
1,096
251
83
Maine, USA
Roblox
CallMeKY
Discord
CallMeKY#5012
i need help with making the StarterCharacters random. something like placing the characters in a folder and a script that chooses whitch one to be the character randomly every time someone respawns or joins.

The easiest way I can think of to do this is a little hacky, but it works. First manually disable the CharacterAutoLoads property of Players. Then put a folder containing all your starter characters in ServerStorage. Then basically destroy any existing starter character and clone a random character to StarterPlayer right before you respawn the player.
Code:
local initialSpawnTime = 1
local respawnTime = 5

local starterPlayer = game:GetService('StarterPlayer')
local starterCharacters = game:GetService('ServerStorage'):WaitForChild('StarterCharacters'):GetChildren()
local connection

math.clamp(respawnTime, 3, math.huge) -- because of the 3 second wait in the died event

local function spawnCharacter(player)
    if starterPlayer:FindFirstChild('StarterCharacter') then
        starterPlayer.StarterCharacter:Destroy() -- destroys any existing starter character
    end
    local newCharacter = starterCharacters[math.random(1, #starterCharacters)]:Clone() -- selects a random character to clone
    newCharacter.Name = 'StarterCharacter' -- this needs to be named StarterCharacter to work
    newCharacter.Parent = starterPlayer -- sets the parent of the random character to starter player
    player:LoadCharacter() -- method to load character
end

local function playerDied(player)
    local waited = wait(3) -- waits 3 seconds and gets the actual time waited
    player.Character:Destroy()-- destroy character
    wait(respawnTime - waited)-- waits for respawn time minus time previously waited
    spawnCharacter(player) -- spawns character
end

game:GetService('Players').PlayerAdded:Connect(function(player)
    local connection -- i think this auto disconnects anyway, but i couldn't remember
    player.CharacterAdded:Connect(function(character)
        if connection then connection:Disconnect() end -- if connection exists, disconnect
        connection = player.Character:WaitForChild('Humanoid').Died:Connect(function() -- connects to the Died event
            playerDied(player) -- run playerDied on death
        end)
    end)
    wait(initialSpawnTime) -- you could also set this up to a play button remote event or something instead of a wait
    spawnCharacter(player) -- initial character spawn
end)
 
  • Like
Reactions: sandro480
General
Help Users
  • No one is chatting at the moment.
    CosmicDev CosmicDev: ERRRRRRRRRRRRRRRRRRRRRRReeeeeeeeeeeeeeeeeeeeeeeeex