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 make it so it chooses a number 1, 2 or 3, and then does a different function...?

How do I make it so it chooses a number 1, 2 or 3, and then does a different function depending on that number?

game.Players.PlayerAdded:connect(function(player)
	local randomNumber = math.random(1, 2, 3)
	if randomNumber == 1 then
		--Code
	end
	if randomNumber == 2 then
		--Code
	end
end)

Thanks!

1 answer

✓ Accepted answer

You're using math.random wrong

math.random only accepts two arguments, not three.

game.Players.PlayerAdded:connect(function(player)
    local randomNumber = math.random(1, 3)
    if randomNumber == 1 then
        --Code
    end
    if randomNumber == 2 then
        --Code
    end
end)

Refinements

Use elseif. Don't keep using if statements. This makes the script shorter and cleaner.

game.Players.PlayerAdded:connect(function(player)
	local randomNumber = math.random(1, 3)
	if randomNumber == 1 then
		--Code
	elseif randomNumber == 2 then
		--Code
	end
end)

Good Luck!

If I helped, please don't forget to accept my answer.

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