2

I have a script for the Roblox version of Lua. Roblox's syntax checking system says that there is nothing wrong with my script. The script is supposed to create a graph of a circle using 'Parts' or 'Bricks'. Below is the wiki page I got the graph function from.

I think bounds.from is the current position of the brick; bounds.to is the next calculated position of the next brick; bounds.step is the counter for what step is being taken - meaning you can change the resolution of the graph (Like 1,000 points or 10,000 points)

Wiki page for graph function. The 'Making a Grapher' is what I used.

local p = Instance.new("Part")
p.Anchored      = true
p.BottomSurface = "Smooth"
p.TopSurface    = "Smooth"
p.BrickColor    = BrickColor.Black()
p.FormFactor    = "Custom"
p.Size          = Vector3.new(1, 1, 1)

function graph(bounds, f)
    bounds.step = bounds.step or (bounds.max - bounds.min) / bounds.n
    for t = bounds.from, bounds.to, bounds.step do
        local xyz = f(t)
        local p = p.clone()
        p.CFrame = CFrame.new(xyz)
        p.Parent = game.Workspace
    end
end

graph({from = 0, to = math.pi * 12, n = 1000}, function(t)
    return Vector3.new(
        5 * math.cos(t),
        5 * math.sin(t),
        0
    )
end)

PS: I am in Algebra 1 so I don't know sines, cosines, and tangents nor parametric equations.

Rontron
  • 3,963
  • 7
  • 26
  • 44
  • Well, what _is_ wrong with it? What do you expect to happen? What happens instead? – Colonel Thirty Two Mar 12 '15 at 20:47
  • The script is supposed to graph a circle using 3D bricks in the Roblox game engine. Instead nothing happens. – Rontron Mar 12 '15 at 23:33
  • Hey, could you give us a screenshot of what it's drawing? I'm at work and don't have access to studio - though I may be able to help if you post a picture. – Henry Mar 13 '15 at 15:36
  • It doesn't draw anything. No errors are reported in the Output console and no bricks are created within the workspace. I click play and the baseplate stays a baseplate. – Rontron Mar 13 '15 at 16:09
  • I uninstalled and reinstalled Roblox studio. This fixed part of the problem. I am now getting an error as shown below. I'm not sure what to do. `12:18:07.320 - Workspace.Script:10: attempt to index field 'max' (a nil value)` – Rontron Mar 13 '15 at 16:20

1 Answers1

1

While looking over the code initially, I noticed quite a few syntax errors, so I looked at the code on the Roblox Wiki, and was surprised to see that the code you've provided matches theirs.

Setting aside the fact that the Roblox Wiki is maintaining broken code, let's take a look at why your code isn't working, and what you can do to fix it.

I'll start with the problem you're having, which originates from the statement bounds.step = bounds.step or (bounds.max - bounds.min) / bounds.n
(Because of how Lua works, the interpretor reads this as (bounds[max] - bounds[min]) / bounds.n, as table.value is syntactic sugar for table[value], and we never defined bounds.max or bounds.min, which is why you're getting the error.)

The proper way to evaluate that expression is to call math.max() and math.min() with explicit arguments, like so: (math.max(bounds.from, bounds.to) - math.min(bounds.from, bounds.to)) / bounds.n, as neither is coded to be used as a method, and can only take integers/floats as parameters anyway; tables are not permissible, and will return an error.

A second error I noticed, and one that would have shown up after fixing the above issue, was that for cloning the template part (defined as local p in the Global scope), the original code's writer used p.clone(), which actually returns this error: 01:49:37.933 - Expected ':' not '.' calling member function clone

The returned response for this error is pretty self-descriptive, but I'll say it anyways: in order to fix this error, simply replace local p = p.clone() with local p = p:clone() inside the graph function.

In case that explanation was too confusing, or you just didn't feel like reading it, I've provided the fixed code (in-full) down below.

Happy coding.


TL;DR, or Fixed code:

local p = Instance.new("Part")
p.Anchored      = true
p.BottomSurface = "Smooth"
p.TopSurface    = "Smooth"
p.BrickColor    = BrickColor.Black()
p.FormFactor    = "Custom"
p.Size          = Vector3.new(1, 1, 1)

function graph(bounds, f)
    bounds.step = bounds.step or (math.max(bounds.from, bounds.to) - math.min(bounds.from, bounds.to)) / bounds.n
    for t = bounds.from, bounds.to, bounds.step do
        local xyz = f(t)
        local p = p:clone()
        p.CFrame = CFrame.new(xyz)
        p.Parent = game.Workspace
    end
end

graph({from = 0, to = math.pi * 12, n = 1000}, function(t)
    return Vector3.new(
        5 * math.cos(t),
        5 * math.sin(t),
        0
    )
end)
  • Thank you for the help! I read your entire post and understood the problem. I am making a program that graphs trade currency rates using a separate python-based web scraper and HttpService. I will try to convert this script to use GUIs instead so it is easier. It will be part of Robux Academy - a series of 'Get Rich' pay-to-access games if you want to see this in action in a few months. – Rontron Mar 14 '15 at 14:32
  • I want to make each point more visible on the graph because 13.742 as an X position (In Workspace, not StarterGUI) is not very far from 13.746. I think subtracting the whole number (In this case, 13) from the number and then multiplying it by 10 or more would make it more visible. I am not sure how to do this though. – Rontron Mar 14 '15 at 15:44
  • @GShocked If you need additional help, try posting in the following thread on the ROBLOX forums: [Game Design](http://www.roblox.com/Forum/ShowForum.aspx?ForumID=40). There are many people (including myself) willing to provide help to those who ask. – Aaron Knott Mar 16 '15 at 05:18