How to utilise Brackets in the same way as math

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I would like to use brackets (also known as “()”) in the same way as math.

  1. What is the issue? Include screenshots / videos if possible!

When a number is placed next to a bracket, the invisible multiplication sign is not accounted for and the code errors.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I tried to put brackets around the whole thing but it did not work and I could not find any solutions on the Developer Hub.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!


local Event = game:GetService("ReplicatedStorage").Events.CalculateEquation

Event.OnServerEvent:Connect(function(player, text, X) -- assume text is "1(X)"
	text = string.gsub(text, "X", tostring(X or 0))
	
	text = "return ("..text..")"
	
	print(text)
	
	local CalculationFunction, errorMessage = loadstring(text)
	
	print(errorMessage) -- [string "return (1(1))"]:1: Expected ')' (to close '(' at column 8), got '('
	
	CalculationFunction()
end)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

To fix this, we can preprocess the input string to insert explicit multiplication operators where needed before passing it to loadstring.

Modified Code:

local Event = game:GetService("ReplicatedStorage").Events.CalculateEquation

Event.OnServerEvent:Connect(function(player, text, X) -- assume text is "1(X)"
    -- Replace 'X' with its value
    text = string.gsub(text, "X", tostring(X or 0))
    
    -- Insert explicit multiplication operators where needed
    text = string.gsub(text, "(%d)(%()", "%1*%2")  -- e.g. "1(" becomes "1*("
    text = string.gsub(text, "(%))(%d)", "%1*%2")  -- e.g. ")1" becomes ")*1"
    text = string.gsub(text, "(%))(%()", "%1*%2")  -- e.g. ")(" becomes ")*("
    
    text = "return ("..text..")"
    
    print(text)  -- Debugging output to see the final expression
    
    local CalculationFunction, errorMessage = loadstring(text)
    
    if errorMessage then
        print(errorMessage)  -- Output the error message for debugging
    else
        local result = CalculationFunction()
        print(result)  -- Print the result of the calculation
    end
end)
1 Like

It seems to work, but could you explain all what each of the “%” do? It would really help me to understand things better in the future, thank you so much!

Summary

  • %d: Matches any digit.
  • %(: Matches an opening parenthesis.
  • %): Matches a closing parenthesis.
  • %1, %2, etc.: Refer to the first, second, etc., captured groups in the pattern.
1 Like

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