So recently, I’ve been thinking of creating a game based on Agatha Christie’s “And Then There Were None” Poem/screen write, so I’ve been messing around with some code in order to choose a random player to become the murderer (The “Riddler” in my case to keep it roblox friendly) however I’ve reached a point where I have no idea why it is erroring.
_G.PlayerRiddler = nil
for i,v in pairs(game.Players:GetPlayers()) do
if i == Random:NextInteger(1,#game.Players:GetPlayers()) then
_G.PlayerRiddler = v.Name
end
end
Is the code I have currently, yet I get an error stating
[ServerScriptService.Core:5: attempt to call method 'NextInteger' (a nil value)]
Do take in mind that I have heard that math.random() is unreliable therefore I have been using Random:NextInteger()
I am frankly still a noob at coding so I will need to do a lot of research to finish this game, but this is just the start, so if I could possibly get some pointers on how to either fix this or a better way on selecting a person (I want to avoid bool values, since they are quite exploitable) that would be amazing! Thanks !
_G.PlayerRiddler = nil
for i,v in pairs(game.Players:GetPlayers()) do
if i == Random.new():NextInteger(1,#game.Players:GetPlayers()) then
_G.PlayerRiddler = v.Name
end
end
The speed difference between math.random and the Random object is negligible, so this isn’t a point of interest to remark on.
The main difference is the capabilities of each. math.random is a standalone function in the math library, while the Random object sports a selection of API you can use, up to branching results and keeping a seed relative to an object rather than globally, which is what math.randomseed(x) implies.
math.random isn’t unreliable, you’re right there. math.random and Random’s algorithms are the same, you’re right there. Random is typically regarded as much more powerful though and speed is completely irrelevant. That’s heading towards microoptimisation grounds.
Please don’t do this. You’re regenerating the random number for each player, so it’s possible that nobody is marked as the killer. Instead, generate the number outside the loop and then compare it to i.
In fact, you don’t even need a loop here. You can just take a random player from a table.
For example:
local rand = Random.new()
local players = game:GetService("Players"):GetPlayers()
local selectedPlayer = players[rand:NextInteger(1, #players)]
_G.PlayerRiddler = selectedPlayer.Name
Random library does have NextNumber, which is much easier and nicer looking than the math.random alternative. The API is also much nicer to look at, even if you can’t do NextInteger(#Whatever).
As you can see I tested it up to 5000 and found little difference between.
In conclusion neither is better.
(Sorry for the bad test grid [paint.net lol] )
He stated that their algorithms are the same, so your test only proved what he was saying, not to the contrary. He only states that Random provides an object with which can have an individual seed, rather than math.random’s global seed along with other advantages.