Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Help on math.random?

Asked by 10 years ago

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.

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

2 answers

Log in to vote
0
Answered by 10 years ago

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
Ad
Log in to vote
0
Answered by 10 years ago

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

Answer this question