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: Accessing an object with a table value

Previous Thread :: Next Thread 
ExtremeBuilder15 is online. ExtremeBuilder15
Joined: 01 May 2012
Total Posts: 3176
30 Nov 2016 05:56 PM
I'm having trouble remember how to get an object by using a table value, for instance:

local tab = {part}
local Part = game.Workspace.tab[1]

is what I'm trying to accomplish, but I don't know or can't remember how it's done. Any help is appreciated!
Report Abuse
Wrathsong is online. Wrathsong
Joined: 05 Jul 2012
Total Posts: 22393
30 Nov 2016 05:59 PM
part is undefined but

local tab = {part}
local Part = workspace[tab[1]]


Ever wanted to learn how to script? Check out my YouTube channel: youtube.com/austintheslayer Want to see me code in action? Follow my twitch: twitch.tv/austinrblx
Report Abuse
ExtremeBuilder15 is online. ExtremeBuilder15
Joined: 01 May 2012
Total Posts: 3176
30 Nov 2016 06:03 PM
That's what I was originally doing, but it kept erroring out, saying it expected a string but got an object

local plrs = {}

game.Players.PlayerAdded:connect(function(plr)
table.insert(plrs,plr)
end)

function roundStart()

for i=1,#plrs do
print(plrs[i])
print(game.Players[plrs[i]])
end
end

Why does it do this?
Report Abuse
Wrathsong is online. Wrathsong
Joined: 05 Jul 2012
Total Posts: 22393
30 Nov 2016 06:07 PM
game.Players.PlayerAdded:connect(function(plr)
plrs = game.Players:GetPlayers()
end)


Ever wanted to learn how to script? Check out my YouTube channel: youtube.com/austintheslayer Want to see me code in action? Follow my twitch: twitch.tv/austinrblx
Report Abuse
ExtremeBuilder15 is online. ExtremeBuilder15
Joined: 01 May 2012
Total Posts: 3176
30 Nov 2016 06:08 PM
I still get the same error with that method :(
Report Abuse
caca50 is not online. caca50
Joined: 10 Jul 2011
Total Posts: 2037
30 Nov 2016 06:18 PM
function roundStart()

for _,v in pairs(game.Players:GetPlayers())do
print(v:GetFullName())
--v is the player
end
for i=1,#plrs do
print(plrs[i])
print(game.Players[plrs[i]])
end
end


Roblox.Forum.Scripters:1: bad argument #1 to 'intellect' (number expected, got nil)
Report Abuse
caca50 is not online. caca50
Joined: 10 Jul 2011
Total Posts: 2037
30 Nov 2016 06:18 PM
Whoops get rid of the second for loop. I was using that for reference to your script


Roblox.Forum.Scripters:1: bad argument #1 to 'intellect' (number expected, got nil)
Report Abuse
Mister_Manakin is not online. Mister_Manakin
Joined: 16 Sep 2009
Total Posts: 1935
30 Nov 2016 06:21 PM
if tab is an array of instances, rather than names, you use
local Part = tab[1]

if tab is an array of string names, use

local Part = workspacce[tab[1]]
Report Abuse
ExtremeBuilder15 is online. ExtremeBuilder15
Joined: 01 May 2012
Total Posts: 3176
02 Dec 2016 08:30 AM
I've figured out the issue to my problem. When I added players to the players table, it was referencing the game.Players.Player1 object, not the player string. I've fixed this by changing the for loop to this:

for i=1,#plrs do
local pString = tostring(plrs[i])
print(plrs[i])
print(game.Players[pString])
end

Now everything works just as expected. If you can think of a better way to do this, I'd love to know! Thanks to everyone who helped my thought process :P
Report Abuse
TimeTicks is not online. TimeTicks
Joined: 27 Apr 2011
Total Posts: 27115
02 Dec 2016 10:03 AM
@OP

GetPlayers already returns a table.

local players = game.Players:GetPlayers()

for i,v in next, players do
print(v.Name)
end


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