Issue with converting chat message to equation

I’m trying to make it so the player can write equations i the chat, and the script can process them and graph it. Right now, I’ve gotten these steps down:

Chat message: “//3x^2”

Remove // “3x^2”

Replace x with number “3(25)^2”

I don’t know where to go from this point on. I tried using tonumber() to calculate this string, but it spits out nil.

It prints out “nil” because you have parenthsis and “^”; to numbers can only be a string including only numbers. Try seperating the string into muiltiple numbers then adding the equations back and calcuating it. :slight_smile:

Should I use a for loop for that? Like it cycles through each character and depending on what it is, it’ll save it in a certain way?

local y = string.gsub(Fx,"x","("..x..")")
local Calculate = loadstring(y)
local Result = Calculate()
print(Result)

I tried this (and I confirmed loadstring() is not exploitable in this context) but it says that Calculate() is a nil value “Workspace.Script:22: attempt to call a nil value”

Do you want something like this?

Luv0qHXxHC
Enable .LoadStringEnabled on ServerScriptService

Server:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local remotes = ReplicatedStorage.Remotes
local stringToFunction = remotes.stringToFunction

local function isValidExpression(expression)
	return expression:match("^[%d%.%+%-%*/%^%(%)x ]+$")
end

local function parseExpression(expression)
	if not isValidExpression(expression) then
		warn("Invalid expression:", expression)
		return nil
	end

	local func, err = loadstring("return function(x) return " .. expression:gsub("x", "*x") .. " end")

	if func then
		return func()
	else
		warn("Error in parseExpression:", err)
		return nil
	end
end

stringToFunction.OnServerEvent:Connect(function(player, receivedString)
	print(receivedString)
	local messageFunction = parseExpression(receivedString)
	if messageFunction then
		local xValue = 25
		local success, result = pcall(messageFunction, xValue)

		if success then
			print("Result of messageFunction:", result)
		else
			warn("Error executing messageFunction:", result)
		end
	end
end)

Client:

local TextChatService = game:GetService("TextChatService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local remotes = ReplicatedStorage.Remotes
local stringToFunction = remotes.stringToFunction


TextChatService.MessageReceived:Connect(function(textChatMessage: TextChatMessage)
	local message = textChatMessage and textChatMessage.Text
	if not message then return end
	stringToFunction:FireServer(message)
end)

If you want to “graph” it, you can do something like this.
If this is not what you wanted, please explain further.


Server:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Workspace = game:GetService("Workspace")
local CollectionService = game:GetService("CollectionService")

local remotes = ReplicatedStorage.Remotes
local stringToFunction = remotes.stringToFunction

local function isValidExpression(expression)
	return expression:match("^[%d%.%+%-%*/%^%(%)x ]+$")
end

local function parseExpression(expression)
	if not isValidExpression(expression) then
		warn("Invalid expression:", expression)
		return nil
	end

	local func, err = loadstring("return function(x) return " .. expression:gsub("x", "*x") .. " end")

	if func then
		return func()
	else
		warn("Error in parseExpression:", err)
		return nil
	end
end

stringToFunction.OnServerEvent:Connect(function(player, receivedString)
	print(receivedString)
	local messageFunction = parseExpression(receivedString)
	if messageFunction then
		for _, v in ipairs(CollectionService:GetTagged("GraphPart")) do
			v:Destroy()
		end
		for x = -20, 20, 0.5 do
			local success, result = pcall(messageFunction, x)
			if success then
				print("Result of messageFunction:", result)
				local newPart = Instance.new("Part", Workspace)
				newPart.Anchored = true
				newPart.CanCollide = false
				newPart.Color = Color3.fromRGB(0, 255, 0)
				newPart.Material = Enum.Material.Neon
				newPart.Size = Vector3.new(1, 1, 1)
				newPart.Position = Vector3.new(x, 0.5, result)
				newPart.Name = tostring(x)
				newPart:AddTag("GraphPart")
			else
				warn("Error executing messageFunction:", result)
			end
			task.wait()
		end
	end
end)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.