Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do i randomise the 'StarterCharacter' for players?

Hi, for my game i require players appearance to be randomised on respawn. I have 4 different characters for my game to choose from. So far i have this:


game.Players.PlayerAdded:connect(function(plr)
	plr.CharacterAdded:connect(function(char)
		local Characters = game:GetService("ReplicatedStorage").Players:GetChildren()[math.random(1,4)]
		print(Characters)
		
		if game:GetService("StarterPlayer"):FindFirstChild("StarterCharacter") then
			game:GetService("StarterPlayer").StarterCharacter:Destroy()
		end
		Characters.Name = "StarterCharacter"
		Characters.Parent = game:GetService("StarterPlayer")
		
		
	end)
end)

Now this works, but not very accurate. Heres what happens in a nutshell.

  1. Spawning (As my own avatar, SHOULD spawn as one of the 4 characters)
  2. Reset
  3. Spawn as ONE of the characters
  4. Reset
  5. Spawn as my Own avatar again.

What i want from this script is so that it will ALWAYS spawn as one of the 4 characters. Please help, Thanks :)

1 answer

✓ Accepted answer

If you check well, you are inserting your rig in StarterPlayer. You're changing it when character is added, which won't put the character you randomised. To fix, you need to change the StarterCharacter when the Character is removed.

game.Players.PlayerAdded:connect(function(plr)
    local Characters = game:GetService("ReplicatedStorage").Players:GetChildren()[math.random(1,4)]

    if game:GetService("StarterPlayer"):FindFirstChild("StarterCharacter") then
        game:GetService("StarterPlayer").StarterCharacter:Destroy()
    end

    Characters.Name = "StarterCharacter"
    Characters.Parent = game:GetService("StarterPlayer")
    plr.CharacterRemoving:connect(function(char)
        Characters = game:GetService("ReplicatedStorage").Players:GetChildren()[math.random(1,4)]

        if game:GetService("StarterPlayer"):FindFirstChild("StarterCharacter") then
            game:GetService("StarterPlayer").StarterCharacter:Destroy()
        end
        Characters.Name = "StarterCharacter"
        Characters.Parent = game:GetService("StarterPlayer")
    end)
end)

10 comments

0

And to do this i will need to add an OnDied function?

0

I've already posted the solution. OnDied is not a existing event. Though, having the character removed is not the same as having 0 health in your humanoid (dead). So, uh, you should use CharacterRemoving.

0

Ok thanks, it works better now. However, How would i randomise the character so that on spawn the player has one of the randomised characters, because at the moment the player still spawns as him/herself as soon the play button is pressed

0

Edited, try it out.

0

Brilliant, Thankyou

0

Now you have corrected it i can now understand the logic, quite a simple fix all along :P. Thankyou so much.

0

No problem

0

Can I use this script for my own game?

0

Also, where do you put the script/localscript ?

0

The Server Script Service i think

This discussion is preserved as it was. New answers, comments, and votes are closed.