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

Im Testing Out math.Random() and I cant seem to do any thing :/ Please Help?

Asked by 10 years ago

Im Testing Out math.Random() and I cant seem to do any thing :/ Please Help?

local players = game.Players:GetPlayers()
zombie = math.random(1, #players)
arm =  zombie:FindFirstChild("Left Arm")
arm:Remove()

0
I dont get the 2 problem could you tell me how I would use that in my script? dscpcheatsis10123 0 — 10y

2 answers

Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

Your code is close to right.

There are two problems:

  • If it worked, zombie would be a Player. Those are the things sitting in the Players service and have things like TeamColor and BackPack. The physical Parts like the arms are in the corresponding Character.

We would fix this by doing something like

zombie = zombie.Character
  • math.random returns a number. A number isn't a Player. Since we have a list of players, and each thing in a list has a corresponding number (1st, 2nd, 3rd...) we use this number with players:
zombie = players[ math.random(1, #players) ]

If you're unfamiliar with how tables work, we ask for the nth thing in a list by using tab[n]. E.g., list[1] is the first thing in a list and list[#list] is the last (in a list of 5 things, the 5th element is the last one)

So these two fixes together will make this work.


As RoboFrog suggested, though, if this is just the contents of a script in a place you publish, it won't do anything, since it will run before any players join. If this is the contents of an event or part of a larger script, this will work fine (as long as there is at least one player)

Ad
Log in to vote
-1
Answered by
RoboFrog 400 Moderation Voter
10 years ago

How is the script being called upon? Without a function of some sort, this code is useless to the computer.

Answer this question