Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0
DenyPhaze

changing a table to a string

Question

so im trying to use the computronics radar that can provide a table showing the name of any players in the radars range and their distance. i want to then cross reference the names of those people and their distance from the radar to provide a redstone output when certain players are within a set range. i can get the radar to show the table however im not sure how to extract the names and distance data from said table to a string so i can cross reference it. 

below is a really quick example of what i mean. it outputs the data however its in a table format and when i try to print it. it just prints what i assume is the table name

https://gyazo.com/ddc63338fba2eed76fdea1a63d8ce38a

Link to post
Share on other sites

2 answers to this question

Recommended Posts

  • 0

You cannot print a table like that. To understand how to extract information from tables you really need to wrap your head around how tables store information, and also how to use the "generic for loop", check out the following links in the official Lua handbook, which although gives a good description contains a lot of computer science jargon that only programmers understand:

https://www.lua.org/pil/4.3.5.html
https://www.lua.org/pil/2.5.html

Both concepts took me quite the amount of time to understand, so in the meantime try to use print(component.radar.getPlayers.name) or print(component.radar.getPlayers.distance). These commands return the values associated to the keys "name" and "distance" respectively, and you can do this with any table entry. Hope this helps! 

Link to post
Share on other sites
  • 0

From the screen shot, it actually appears that the `getPlayers()` function is returning an array (a type of table), so while _Jacques is being helpful, the code sample they provided won't work. However, I do highly recommend you read the lua.org links _Jacques provided. Those will definitely be good to read

try this

local component = require("component")
for _, player in ipairs(component.radar.getPlayers()) do
	print(player.name, player.distance)
end

 

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.