[READ REPLIES] Help with math.random

So i’m working on a game where I want some people to be red some people to be blue, and some green. I’m trying to do that using math.random, yet there’s two problems…

  • With the script below it’s only giving GREEN avatars (you can see my humanoid descriptions below) however I want it to give either of the three.

  • I feel this isn’t even efficient enough for what I want, is there anyway to make 1/3 red, 1/3 blue, and 1/3 green?

Any help would be appreciated :^)

game.Players.CharacterAutoLoads = false
local avs = game:FindService("ReplicatedStorage").ColorsAvs.Default:GetChildren()
local int = math.random(#avs)
local randav = avs[int]
 
local function onPlayerAdded(player)
	if player.Name == "ollieryz" then
		player:LoadCharacterWithHumanoidDescription(game.ReplicatedStorage.ColorsAvs.VIPAv)
	else
    	player:LoadCharacterWithHumanoidDescription(randav)
    end
end
 
game.Players.PlayerAdded:Connect(onPlayerAdded)

image

You have to randomize it inside your onPlayerAdded function. Right now it should be random, but it’ll only randomize once - so everyone who joins your game gets the same color

2 Likes

Ahhh, yes, my bad, I will fix that - I have another issue - the re-spawning of players.

player.CharacterAdded:Connect(function(chr)
	chr.Humanoid.Died:Connect(function()
		wait(3)
		player:LoadCharacter()
	end)
end)

I’ve tried this, however it doesn’t work.

1 Like

In your script, you only choose the colour once. So when a player joins, it will return the same color for all players who join. As for it being only green, I think thats because you only set a maximum number, and not a minimum. So it returns the last value in the array

Simplified

Add the random choosing into the OnPlayerAdded function, and then add the number 1 followed by #avs in your int variable.

Edit: Someone got to the function part before me, but yeah, still add a minimum

1 Like

1 Like

No worries! All players go into a game with a bool called “CanLoadCharacterAppearence”. Just switch it off and you’re all good. If you’re tying to set the respawn time, thats also changeable in the humanoid as well as the location. The respawn time is set to 7 by default.

1 Like

As you can see by my script ive done game.Players.CharacterAutoLoads = false, meaning they shouldn’t be respawning, I don’t quite understand what you’re saying.

For respawn, your problem is that you want them to respawn faster, correct?

no, i just want them to respawn normally, however i disabled respawn

Since you disabled CharacterAutoLoads, Therefore players don’t spawn in the first place.
All you have to do is:

game.Players.PlayerAdded:Connect(function(plr)
     plr.CharacterAdded:Connect(function(char)
          char.Humanoid.Died:Connect(function()
                 wait(yourDuration)
              plr:LoadCharacter()
          end)
      end)
    plr:LoadCharacter() --load em in for the first time
end)

that’s what i’ve done. . . . .

That only respawns them when they die, But doesn’t actually spawn them when they first join.


Sorry, Completely went over that. But, this should work:

local function onPlayerAdded(player)
	if player.Name == "ollieryz" then
		player:LoadCharacterWithHumanoidDescription(game.ReplicatedStorage.ColorsAvs.VIPAv)
	else
    	player:LoadCharacterWithHumanoidDescription(randav)
    end
      player.CharacterAdded:Connect(function(char)
        char.Humanoid.Died:Connect(function()
             wait(3)
           player:LoadCharacter()
    end)
  end)
end

went ahead and tested it and works completely fine.

wh- you haven’t even changed the script I have?

player.CharacterAdded:Connect(function(chr)
	chr.Humanoid.Died:Connect(function()
		wait(3)
		player:LoadCharacter()
	end)
end)