Desmos on roblox not working

So im making a game on roblox that graphs a function that you put in a textbox, pretty simple right? i cant code.

The script workis fine when i just put the function in the script but it doesnt when i make it get the function from the textbox

1st image is the script when i put the function in it

2nd image is when i do it via the texbox

I have tried doing this differently but it really just wouldnt be how i want it to be and it just doesnt work

1st code block is the one that doesnt work

local textBox = script.Parent

local function plotGraph()
	local frame = script.Parent.Parent
	frame.Size = UDim2.new(1, 0, 1, 0)
	frame.AnchorPoint = Vector2.new(0.5, 0.5)
	frame.Position = UDim2.new(0.5, 0, 0.5, 0)

	local graphWidth = frame.AbsoluteSize.X
	local graphHeight = frame.AbsoluteSize.Y
	local scale = 20
	local originX = graphWidth / 2
	local originY = graphHeight / 2

	local function plotPoint(x, y)
		local point = Instance.new("Frame")
		point.Size = UDim2.new(0, 4, 0, 4) 
		point.Position = UDim2.new(0, originX + x * scale, 0, originY - y * scale)
		point.BackgroundColor3 = Color3.new(1, 0, 0) 
		point.BorderSizePixel = 0
		point.Parent = frame
	end

	for x = -10, 10, 0.1 do
		local y = textBox.Text:match("^%s*(.-)%s*$") --the error has to do with this line apparently
		plotPoint(x, y)
	end
end

textBox.FocusLost:Connect(function(enterPressed)
	if enterPressed then
		plotGraph()
	end
end)

2nd is the working one

local frame = script.Parent 

frame.Size = UDim2.new(1, 0, 1, 0) 
frame.AnchorPoint = Vector2.new(0.5, 0.5) 
frame.Position = UDim2.new(0.5, 0, 0.5, 0) 

local graphWidth = frame.AbsoluteSize.X 
local graphHeight = frame.AbsoluteSize.Y 
local scale = 20 
local originX = graphWidth / 2
local originY = graphHeight / 2

local function plotPoint(x, y)
	local point = Instance.new("Frame")
	point.Size = UDim2.new(0, 4, 0, 4) 
	point.Position = UDim2.new(0, originX + x * scale, 0, originY - y * scale)
	point.BackgroundColor3 = Color3.new(1, 0, 0) 
	point.BorderSizePixel = 0
	point.Parent = frame
end


for x = -10, 10, 0.1 do
	local y = x
	plotPoint(x, y)
end
1 Like

From what I saw, in the 2nd block the value of Y is a number, but in the first, it is a text. Right?

You can try convert the value from string to number. If you are trying to make the input of “X” the same value as the variable X, you can add a condition in the script, so that, if the input is “X”, then use the value of the variable X

1 Like

this does make sense, but how would i put it?

1 Like

To convert a string to a number, simply use “tonumber(string)”

I’ll try to write the code here, maybe it has an error because I’m typing on my phone

Test it and tell me how the result was, if it doesn’t work, send another photo of the console

local textBox = script.Parent

local function plotGraph()
	local frame = script.Parent.Parent
	frame.Size = UDim2.new(1, 0, 1, 0)
	frame.AnchorPoint = Vector2.new(0.5, 0.5)
	frame.Position = UDim2.new(0.5, 0, 0.5, 0)

	local graphWidth = frame.AbsoluteSize.X
	local graphHeight = frame.AbsoluteSize.Y
	local scale = 20
	local originX = graphWidth / 2
	local originY = graphHeight / 2

	local function plotPoint(x, y)
		local point = Instance.new("Frame")
		point.Size = UDim2.new(0, 4, 0, 4) 
		point.Position = UDim2.new(0, originX + x * scale, 0, originY - y * scale)
		point.BackgroundColor3 = Color3.new(1, 0, 0) 
		point.BorderSizePixel = 0
		point.Parent = frame
	end

	for x = -10, 10, 0.1 do
		local y = 0
		local input = textBox.Text:match("^%s*(.-)%s*$") --the error has to do with this line apparently
		if input == "X" or "x" then
			y = x
		else
			y = tonumber(input)
		end

		plotPoint(x, y)
	end
end

textBox.FocusLost:Connect(function(enterPressed)
	if enterPressed then
		plotGraph()
	end
end)
1 Like

ill try it tomorrow bc its 1 am rn and im about to go to bed rq

Ok, here in my country it’s 3:40am!!
I look forward to an answer tomorrow! Good night :slight_smile:

1 Like

this works but it basically just graphs y=x only and any other function doesnt work, might need to manually put all of them in :sob:

(realized i marked the wrong spot for the error but it still seems like an odd error)

local textBox = script.Parent

local function plotGraph()
	local frame = script.Parent.Parent
	frame.Size = UDim2.new(1, 0, 1, 0)
	frame.AnchorPoint = Vector2.new(0.5, 0.5)
	frame.Position = UDim2.new(0.5, 0, 0.5, 0)

	local graphWidth = frame.AbsoluteSize.X
	local graphHeight = frame.AbsoluteSize.Y
	local scale = 20
	local originX = graphWidth / 2
	local originY = graphHeight / 2

	local function plotPoint(x, y)
		local point = Instance.new("Frame")
		point.Size = UDim2.new(0, 4, 0, 4) 
		point.Position = UDim2.new(0, originX + x * scale, 0, originY - y * scale) -- error here
		point.BackgroundColor3 = Color3.new(1, 0, 0) 
		point.BorderSizePixel = 0
		point.Parent = frame
	end

	for x = -10, 10, 0.1 do
		local y = textBox.Text:match("^%s*(.-)%s*$")
		plotPoint(x, y)
	end
end

textBox.FocusLost:Connect(function(enterPressed)
	if enterPressed then
		plotGraph()
	end
end)

I think it’s interesting that you take a look at “loadstrings” search more about it in devforum

ill check it out, might be what i need

checking it out but it doesnt stay on when i start the game

edit: found out it cant be used by the client so it wouldnt work, have checked out modules but idk which i should use

1 Like
local y = tonumber(textBox.Text:match("^%s*(.-)%s*$"))

edit: nevermind thats already fixed

1 Like