So explanation is
I have a string it says
10/10/10+10
ok?
now normally using loadstring the answer would be 10.1 but as loadstring is dangerous I want to use string.split and gsub and sub and idk what more to do that
I want a system to sperate the first 3 tens leaving the last one with the + so
10/10/10+10
after fraction
0.1+10
after addition
10.1
So, I’d say to get the right area of the 10’s string by doing
local fraction = string:sub("10/10/10+10", 1, 9) -- take note, an empty space `""` counts as a character, so you're always starting with 1
then afterwards, now what you can do is separate that fraction string by getting “/”, then afterwards making a math function manually:
local tens = fraction:split("/") -- This will make a table of all of the values that was "splitted".
local theNewFraction = tonumber(tens[1]) / tonumber(tens[2]) / tonumber(tens[3]) + tonumber(string:split("10/10/10+10", 9)) -- This is just using that table and making it into an actual math function.
Using it in an actual code, it should look something like this:
local fraction = string:sub("10/10/10+10", 1, 9)
local tens = fraction:split("/")
local theNewFraction = tonumber(tens[1]) / tonumber(tens[2]) / tonumber(tens[3]) + tonumber(string:split("10/10/10+10", 9))
print(tostring(theNewFraction))
so @edozune and @Super_pro322222 i am sorry to say but both of these methods are not what I want you see I am making a calculator! and it shouldnt use loadstring cause its bad backdoors and stuff anyways the equation is not always 10/10/10+10 it can be different most of the times
lets say a player inputted 10+10-10
now if i do string.split(“10+10-10”,"+") it would return {“10 10-10”} see thats the problem
roblox would just error unless… wait a second HOLD UP
This isn’t something that can be done easily. You will need to learn to write a parser, and it’s not something that can be done in an afternoon. Do a search for arithmetic parser.
well I made this and it sucks at math more than a 3rd grader
local subtab = {}
local addtab = {}
local function solve(input)
local sub = string.split(input,"-")
for i,v in pairs(sub) do
if not string.find(v,"+") then
table.insert(subtab,v)
else
local add = string.split(v,"+")
for _,a in pairs(add) do
table.insert(addtab,a)
end
end
end
local num = 0
for _,a in pairs(addtab) do
num += a
end
for _,s in pairs(subtab) do
num -= s
end
print(num)
end
solve("5-10-10")
Yeah, but you wanted to do it without using a loadstring and I know it’s possible. So, I just wanted to kind of fulfill the initial request, which was doing it without the loadstring.
@Qinrir, JUST FOUND A WAY TO DO IT WITHOUT LOADSTRING.
So, using a module called: Luaxp, it can help with making a string a mathmatical function, and it’ll automatically return as a string back.
Put the module in ReplicatedStorage and put this code inside of your script, modify it to add more conditions and valla!
local parser = require(game.ReplicatedStorage:WaitForChild("MainModule"))
local expression
TextBox = script.Parent.TextBox
if TextBox then
for buttons, button in ipairs(script.Parent:GetChildren()) do
if button:IsA("TextButton") then
if button.Name == "=" then
button.MouseButton1Click:Connect(function()
expression = parser.evaluate(TextBox.Text)
wait()
TextBox.Text = expression
end)
elseif button.Name == "C" then
button.MouseButton1Click:Connect(function()
TextBox.Text = ""
end)
elseif button.Name == "CE" then
button.MouseButton1Click:Connect(function()
TextBox.Text = TextBox.Text:gsub(".?$","")
end)
-- Going to assume you have more buttons like ^, sqr, log, etc. Like how the if statements are set up, make the button name be the symbol, and then put the symbol in as a text.
else
button.MouseButton1Click:Connect(function()
TextBox.Text ..= button.Text
end)
end
end
end
end