Seems fine to me. the code i used is i put the script inside Staterpack with your extra values inside folders in the same Starterpack object.
Folder Structure:
Game
|
\
\
\
\
\
\
\
Code:
wait(2)
local jawaban
local b1 = script.parent.B1.B
local b2 = script.parent.B2.B
local oper = script.Parent.O:FindFirstChildWhichIsA("BoolValue")
local pilihan
local function calculate()
if oper.Name == "x" then
jawaban = b1.Value * b2.Value
elseif oper.Name == "+" then
jawaban = b1.Value + b2.Value
elseif oper.Name == "-" then
jawaban = b1.Value - b2.Value
end
print(jawaban)
end
calculate()
Output:
0
The only thing I can think why you're getting different values for your sum object is grabbing the value on script run, theres a chance when you grab the "live" values in your calculate() function these values change.
Example:
ObjA.Value = 0
local ObjA = script.Parent.ObjA
local ObjB = script.Parent.ObjB
local a = ObjA.Value
local b = ObjB.Value
Another script Connected to these values can change the values before your calculate() function has chance to run
Another script:
local ObjA = script.Parent.ObjA
ObjA.Value = 1
Your calculate function() reads these values as 0 since its recoded on run.
Whereas:
ObjA.Value = 0
local ObjA = script.Parent.ObjA
local ObjB = script.Parent.ObjB
local a = ObjA
local b = ObjB.Value
Other script:
local ObjA = script.Parent.ObjA
ObjA.Value = 1
Your calculate function() reads these as it executes and it sees ObjA.Value as 1 since another script has changed it
a little spin on your code to make it a bit more user-friendly. Though it does make it a bit more complex lol.
Code:
wait(2)
local jawaban
local b1 = script.parent.B1.B
local b2 = script.parent.B2.B
local oper = script.Parent.O:FindFirstChildWhichIsA("BoolValue")
local pilihan
local execute = {
Opers = {
['+'] = function (x, y) return x + y,"Pass" end,
['-'] = function (x, y) return x - y,"Pass" end,
['/'] = function (x, y)
if(x == 0 or y == 0) then
return 0, "Divide by Zero Event!"
end
return x / y, "Pass"
end,
['*'] = function (x, y) return x * y,"Pass" end,
}
}
function execute:DoDaMath(x,y,op)
local chk = self.Opers[op]
if(chk ~= nil) then
return self.Opers[op](b1.Value, b2.Value)
end
return "Error!","Error ["..op.."] is not Valid!"
end
local function calculate()
local jawaban,ErrorCode = execute:DoDaMath(b1.Value,b2.Value,oper.Name)
print(jawaban,ErrorCode)
end
calculate()
The code returns 2 values, the result from your two sums and an Error code, the error code returns if the sum works and if there's a case for the dreaded divide by zero. It also checks to see if your operator is also valid.