Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Help on math.random?

im making a game but this script wont work:

local Player = Game.Players.LocalPlayer
local  Hum = script.Parent
local Chance = Players[math.random(1,15)]

Theres more but this is where I have problems.

1 comment

0

Have you tried rounding the random number? There's no "Player 2.3562471345682" which is essentially what math.random() produces.

2 answers

✓ Accepted answer

Its because you do math.random(1,15) and Players must be game.Players not just Players and because LocalScripts can only get LocalPlayer

make a server-side script and add this code. name the script "F1"

F1:

game.Players.PlayerAdded:connect(function(Player)
	Player.CharacterAdded:connect(function(Character)
		local Humanoid = Character:WaitForChild("Humanoid") -- name of Players humanoid
		local F2 = script.F2:Clone()
		F2.Parent = Humanoid
		F2.Disabled = false
	end)
end) 

now make another server-side script and parent it to F1 make sure F2 is disabled F2:

local Hum = script.Parent
local Character = Hum.Parent
local Player = game.Players:GetPlayerFromCharacter(Character)
local Chance = game.Players[math.random(1, #game.Players:GetChildren())] -- #game.Players:GetChildren() gets the total amount of Players in game.Players

Well first off, you haven't defined what Players is. Second, if you're trying to select a random player in the game, then math.random(1,15) wouldn't always work, because there won't always be 15 players in the server. This would be the correct way to do it:

local Player = game.Players.LocalPlayer
local Hum = script.Parent
local Players = game.Players:GetPlayers()
local Chance = Players[math.random(1,#Players)]

NOTE: #Players is the size of Table Players, or basically how many players there are in the server

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