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

Share this post


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! 

Share this post


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

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

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