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: having trouble selecting a random player

Previous Thread :: Next Thread 
smartlegomaniac is not online. smartlegomaniac
Joined: 16 Jun 2012
Total Posts: 411
13 Dec 2016 04:17 PM
I'm trying to select a random player and add a picture of their character to and imagelabel and add their account name to a textlabel. The script can only be tested if you have more than one player. I get this error when I test it:

: attempt to call local 'players' (a table value)

This is my script:

local players = game.Players:GetPlayers()

repeat wait() until #players > 1
local target = players(math.random(1, #players))
script.Parent.Image = "http://www.roblox.com/Thumbs/Avatar.ashx?x=100&y=100&Format=Png&username="..target.Name
script.Parent.TextLabel.Text = target.Name
for i=1, 5 do
script.Parent.Position = script.Parent.Position + UDim2.new(0,41,0,0)
wait()
end
Report Abuse
HalfPinky456 is not online. HalfPinky456
Joined: 10 Apr 2011
Total Posts: 6236
13 Dec 2016 04:20 PM
change outer parentheses in to brackets
Report Abuse
AggressiveCatch is not online. AggressiveCatch
Joined: 17 Jul 2011
Total Posts: 5840
13 Dec 2016 04:20 PM
local target = players[math.random(1, #players)]

square brackets not round brackets
Report Abuse
Bobby_Darin is not online. Bobby_Darin
Joined: 05 Jan 2013
Total Posts: 5587
13 Dec 2016 04:22 PM
You use brackets to index a table not parenthesis

local table = {x = 5}

To index x it would be table[x] not table(x)
Report Abuse
smartlegomaniac is not online. smartlegomaniac
Joined: 16 Jun 2012
Total Posts: 411
13 Dec 2016 04:26 PM
It's working now, thanks.
Report Abuse
smartlegomaniac is not online. smartlegomaniac
Joined: 16 Jun 2012
Total Posts: 411
13 Dec 2016 05:03 PM
I probably should've made a new thread for this, but...

I'm tring to make the getplayer function repeat every 10 seconds if target = nil. The getplayer function is working however, the while do part is not. I haven't gotten any errors. Anyone know what's wrong with the script?

players = game.Players:GetPlayers()

function getplayer(getplayer)
repeat wait() until #players > 1
repeat
target = players[math.random(1, #players)]
until target.Name ~= game.Players.LocalPlayer.Name
script.Parent.Image = "http://www.roblox.com/Thumbs/Avatar.ashx?x=100&y=100&Format=Png&username="..target.Name
script.Parent.TextLabel.Text = target.Name
for i=1, 5 do
script.Parent.Position = script.Parent.Position + UDim2.new(0,41,0,0)
wait()
end
end
getplayer(getplayer)

while wait(10) do
if target == nil then
getplayer()
end
end
Report Abuse
FlamedSkull is not online. FlamedSkull
Joined: 23 May 2009
Total Posts: 1379
13 Dec 2016 05:09 PM
:GetPlayers() returns a table of the CURRENT players when it's called. You only did it once so if there was only one player in the game and another came the player list would still be 1.


Report Abuse
newcoolman2 is not online. newcoolman2
Joined: 01 Apr 2010
Total Posts: 2637
13 Dec 2016 05:13 PM
local par=script.Parent --convienence
local target --might want this outside the function
local function getPlayer()
local players=game.Players:GetPlayers() --CURRENT players
repeat wait()
until #players>1
repeat target=players[math.random(#players)]
until target.Name~=game.Players.LocalPlayer.Name
par.Image="http://www.roblox.com/Thumbs/Avatar.ashx?x=100&y=100&Format=Png&username="..target.Name
par.TextLabel.Text=target.Name
for i=1,5 do
par.Position=par.Position+UDim2.new(0,41,0,0) --there is UDim2.new().lerp now
wait()
end
end

while true do --i really do prefer this.
if not target then
getPlayer()
end
wait(10)
end


Report Abuse
newcoolman2 is not online. newcoolman2
Joined: 01 Apr 2010
Total Posts: 2637
13 Dec 2016 05:16 PM
you also should add math.randomseed(tick()) to the top of your script as well since you are dealing with random numbers.


Report Abuse
smartlegomaniac is not online. smartlegomaniac
Joined: 16 Jun 2012
Total Posts: 411
13 Dec 2016 05:22 PM
Thank you so much!
Report Abuse
smartlegomaniac is not online. smartlegomaniac
Joined: 16 Jun 2012
Total Posts: 411
13 Dec 2016 06:01 PM
The getPlayer function still isn't repeating.

This is the script currently:

math.randomseed(tick())
local target

local function getPlayer()
local players = game.Players:GetPlayers()
repeat wait() until #players > 1
repeat target = players[math.random(#players)]
until target.Name ~= game.Players.LocalPlayer.Name
script.Parent.Image = "http://www.roblox.com/Thumbs/Avatar.ashx?x=100&y=100&Format=Png&username="..target.Name
script.Parent.TextLabel.Text = target.Name
for i=1,5 do
script.Parent.Position = script.Parent.Position + UDim2.new(0,41,0,0)
wait()
end
end

while true do
if not target then
getPlayer()
end
wait(10)
end
Report Abuse
TimeTicks is not online. TimeTicks
Joined: 27 Apr 2011
Total Posts: 27115
13 Dec 2016 06:02 PM
why are you using a loop?

use PlayerAdded and PlayerRemoved


Report Abuse
Soybeen is not online. Soybeen
Joined: 17 Feb 2010
Total Posts: 21462
13 Dec 2016 06:06 PM
###############################
Report Abuse
Soybeen is not online. Soybeen
Joined: 17 Feb 2010
Total Posts: 21462
13 Dec 2016 06:07 PM
I swear to god my last half dozen posts have been censored.

Bobby, those are called braces.


Report Abuse
Bobby_Darin is not online. Bobby_Darin
Joined: 05 Jan 2013
Total Posts: 5587
13 Dec 2016 06:59 PM
It doesn't really matter what you call them so long as people know what you're talking about, you're wrong either way because braces typically are 'curly brackets' - {}
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