generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: Length of userdata, help.

Previous Thread :: Next Thread 
Faisal1207 is not online. Faisal1207
Joined: 27 Dec 2013
Total Posts: 499
20 Oct 2015 12:54 PM
function DisplayManager:ClassDisplay()
for _, plrs in pairs(game.Players:GetPlayers()) do
print(#game.Players:GetPlayers())
local gui = plrs.PlayerGui:FindFirstChild("ScreenGui")
local inormDisp = gui:FindFirstChild("ClassDisplayiNormal")
local armedDisp = gui:FindFirstChild("ClassDisplayArmed")
local innoDisp = gui:FindFirstChild("ClassDisplayInnocent")
--local raiderDisp = gui:FindFirstChild("ClassDisplayRaider")
local inorm = plrs[math.random(1,#plrs)]
local rndm = plrs[math.random(1,#plrs)]
repeat wait() rndm = v[math.random(1,#plrs)]
until rndm ~= inorm
local armed = rndm
return inormDisp
end
end

Attempt to get length of local 'plrs' (a userdata value)
How can i work around this?
Report Abuse
morashsPeasant is not online. morashsPeasant
Joined: 06 Jan 2011
Total Posts: 4944
20 Oct 2015 01:01 PM
Hmm weird, but you can usually get the number of players by going like this:

print(game.Players.NumPlayers)
Report Abuse
Faisal1207 is not online. Faisal1207
Joined: 27 Dec 2013
Total Posts: 499
20 Oct 2015 01:12 PM
I call it in another script, and i need to choose randomly from players.



Report Abuse
Faisal1207 is not online. Faisal1207
Joined: 27 Dec 2013
Total Posts: 499
20 Oct 2015 02:25 PM
Bump
Report Abuse
NovusTheory is not online. NovusTheory
Joined: 14 Oct 2009
Total Posts: 4532
20 Oct 2015 02:28 PM
lol, you did it really wrong. Why would you for loop the players when you want a random player?
Report Abuse
Faisal1207 is not online. Faisal1207
Joined: 27 Dec 2013
Total Posts: 499
20 Oct 2015 02:33 PM
If i don't get a new list of players each time, if i like: local players = game.Players:GetPlayers() give me the same value each time its called? I used the for loop as an work around



- "I say no because yes is overrated"
Report Abuse
Faisal1207 is not online. Faisal1207
Joined: 27 Dec 2013
Total Posts: 499
20 Oct 2015 02:45 PM
Bump.



- "I say no because yes is overrated"
Report Abuse
JimmyChance is not online. JimmyChance
Joined: 01 Nov 2009
Total Posts: 3681
20 Oct 2015 02:48 PM
You're using the len operator on a player object.
Report Abuse
NovusTheory is not online. NovusTheory
Joined: 14 Oct 2009
Total Posts: 4532
20 Oct 2015 02:48 PM
You know you can get a random player from the GetPlayers() right?
Report Abuse
Faisal1207 is not online. Faisal1207
Joined: 27 Dec 2013
Total Posts: 499
20 Oct 2015 02:51 PM
Does GetPlayers() have arguments for that




- "I say no because yes is overrated"
Report Abuse
NovusTheory is not online. NovusTheory
Joined: 14 Oct 2009
Total Posts: 4532
20 Oct 2015 02:52 PM
game.Players:GetPlayers()[math.random(1, #game.Players:GetPlayers())]
Report Abuse
JimmyChance is not online. JimmyChance
Joined: 01 Nov 2009
Total Posts: 3681
20 Oct 2015 02:52 PM
local players = game.Players:GetPlayers();
local this = players[math.random(#players)];
Report Abuse
NovusTheory is not online. NovusTheory
Joined: 14 Oct 2009
Total Posts: 4532
20 Oct 2015 02:53 PM
@Jimmy

2sLate
Report Abuse
JimmyChance is not online. JimmyChance
Joined: 01 Nov 2009
Total Posts: 3681
20 Oct 2015 02:54 PM
qq
Report Abuse
Faisal1207 is not online. Faisal1207
Joined: 27 Dec 2013
Total Posts: 499
20 Oct 2015 02:54 PM
Thanks, and if i returned Two random players instead of one how would i assign each player to a variable?





- "I say no because yes is overrated"
Report Abuse
NovusTheory is not online. NovusTheory
Joined: 14 Oct 2009
Total Posts: 4532
20 Oct 2015 02:55 PM
local player = RANDOM_LINE -- That was provided by me

That's literally it
Report Abuse
Faisal1207 is not online. Faisal1207
Joined: 27 Dec 2013
Total Posts: 499
20 Oct 2015 03:01 PM
Lets say plrs are defined as the 2 players it would go like this;

local plr1 = [1,#plrs] -- Im really sleepy, so if i am being stoopid blame the sleeping hormones.. zzzzz

local plr2 = [2,#plrs]




- "I say no because yes is overrated"
Report Abuse
JimmyChance is not online. JimmyChance
Joined: 01 Nov 2009
Total Posts: 3681
20 Oct 2015 03:05 PM
local players = {};
setmetatable(players, {__call = function() return game.Players:GetPlayers() end});

players.random = function(maxn)
local this = {};
local players = players();
while (#players > 0) do
if (#this < maxn) then
this[#this + 1] = table.remove(players, math.random(#players));
end
end
return unpack(this);
end

local player1, player2 = players.random(2);

I normally use this when selecting more than 1 random player.
Report Abuse
JimmyChance is not online. JimmyChance
Joined: 01 Nov 2009
Total Posts: 3681
20 Oct 2015 03:07 PM
local players = {};
setmetatable(players, {__call = function() return game.Players:GetPlayers() end});

players.random = function(maxn)
local this = {};
local players = players();
while (#players > 0) do
if (#this < maxn) then
this[#this + 1] = table.remove(players, math.random(#players));
else break;
-- forgot the break statement.
end
end
return unpack(this);
end

local player1, player2 = players.random(2);
Report Abuse
Faisal1207 is not online. Faisal1207
Joined: 27 Dec 2013
Total Posts: 499
20 Oct 2015 03:19 PM
Thanks both of you!




- "I say no because yes is overrated"



Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image