Math.Random 50/50

use this it will work so fine tho Math.Random 50/50 - #19 by MdoxxxDSQa

1 Like

I don’t get what you mean teleport into folder.

You are telling me there is a big model in your game that looks like a filing folder and you want the character to teleport into it?

To do that all you need to do is set the Characters cframe to the cframe of the big floating Folder Model you want to teleport to.

1 Like

xD he want to move player in Folder not teleporting him

wait ill explain more clear im very sorry
image
these 4 players will teleport into
image
these 2 folder in split like this
image

yes thats what i want i want the player move into folder :slight_smile:

This thread is hilarious Lmao

Okay I just found out it says you can move player characters

I still think there’s no point in moving them around because then when the character respawns you have to move them again but whatever (unless that’s what you want)

All you have to do to parent something in Luau is do

Instance.Parent = OtherInstance

In this case you want to put the Character models in folders so

player.Character.Parent = Folder

Edit: in the picture you sent you have characters under the “players” service, that’s not where they are all characters are in workspace

but i want the player move into 2 folder in split thats why i use math.random my only problem i dont know how to split them

local teleporters = math.random(1,2)

if teleporters == 1 then
	player.Character.Parent = 1folder
end
if teleporters == 2 then
	player.Character.Parent = 2folder
end
1 Like

But when math.Random pick 1 then the player move into 1 folder only

if you want to randomly put half the players characters into 1 folder and half into the other you can make a script like this

local Players = game.Players

local folder1 = workspace.Folder1
local folder2 = worlspace.Folder2

local function onPlayerAdded (player)
   player.CharacterAdded:Connect(function(character)
       local coin = math.random(1,2)

       if coin == 1 then
          character.Parent = folder1
       else
          character.Parent = folder2
       end
   end)
end


for _,player in ipairs (Players:GetPlayers()) do
    task.spawn(onPlayerAdded)(player)
end

Players.PlayerAdded:Connect(onPlayerAdded)

This script just puts a players character into a folder when their character loads (it will do it again if the character respawns)

1 Like

I want to do it in the same time like

The code below will split all the players evenly, or odd depending on the table length:

For your case, you can change the players table to PlayersService:GetPlayers(), and use a and b accordingly.

local players = {'p1', 'p2', 'p3', 'p4'} 
local half = math.ceil(#players / 2) 
local a = {} 
local b = {} 

for i = 1, #players do 
	if (i <= half) then 
		table.insert(a, players[i])
	else 
		table.insert(b, players[i])
	end 
end 

print(unpack(a)) -- -> p1 p2 p3
print(unpack(b)) -- -> p4 p5 p6
1 Like

Listen I don’t know what that means, maybe ask someone that speaks your language.

I supplied you with a simple script that puts the players character into 1 of 2 folders randomly each time the character spawns.

You can change the script to implement it the way you want.

1 Like

As i said i want the player move into 2 folder 50/50 lets say it has 4 player the 2 player will move into folder 1 and the other 2 player will move into folder 2

This is what i want but i just need to recreate this

1 Like

Thank u guys for ur time iit helps me

2 Likes

Okay no problem,

Here’s the last script I made it just puts half the characters in folder1 and half in folder2 when it runs. If more people join it won’t run for them (then you have to use player.CharacterAdded())

local folder1 = workspace.Folder1
local folder2 = workspace.Folder2


for i,player in ipairs(game.Players:GetPlayers()) do
    local character = player.Character
    if character == nil then continue end -- don't run for players that don't have a character
    -- if you want it to run when characters are added you can connect it to a CharacterAdded Event
    
    -- this funny if else statement block just ensures the characters are getting evenly distributed
    if #folder1:GetChildren() < #folder2:GetChildren() then
        player.Character.Parent = folder1
    elseif #folder1:GetChildren() > #folder2:GetChildren() then
        player.Character.Parent = folder2
    elseif #folder1:GetChildren() == #folder2:GetChildren() then
        player.Character.Parent = folder1
    end
end
1 Like

You should use continue instead of return as if the character is nil, then the loop will simply stop. However, if you use continue it will skip that index.

Example return:

local array = {1, 2, 3} 

for i = 1, #array do 
	if (i == 2) then 
		return
	end 
	
	print(i) -- Only prints "1", nothing else
end

Example continue:

local array = {1, 2, 3} 

for i = 1, #array do 
	if (i == 2) then 
		continue
	end 
	
	print(i) -- Only prints "1" and "3", skipping "2"
end
2 Likes

Thanks I forgot about that :+1:

2 Likes

math.random needs integers? Maybe .Value? Sorry, it’s not very clear to me what you are trying to do.

1 Like