Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How do i randomise the 'StarterCharacter' for players?

Asked by 6 years ago

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

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

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)
0
And to do this i will need to add an OnDied function? NoirPhoenix 148 — 6y
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. SulaymanArafat 230 — 6y
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 NoirPhoenix 148 — 6y
0
Edited, try it out. SulaymanArafat 230 — 6y
View all comments (6 more)
0
Brilliant, Thankyou NoirPhoenix 148 — 6y
0
Now you have corrected it i can now understand the logic, quite a simple fix all along :P. Thankyou so much. NoirPhoenix 148 — 6y
0
No problem SulaymanArafat 230 — 6y
0
Can I use this script for my own game? ShockinglyAmazing 0 — 4y
0
Also, where do you put the script/localscript ? ShockinglyAmazing 0 — 4y
0
The Server Script Service i think User#34345 0 — 4y
Ad

Answer this question