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