Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Can someone explain to me what is the difference between variable intValue.Value and intValue?

Asked by 3 years ago

so i've made a script. But there is a problem. The variable b1 and b2 which is a intValue.Value when i run the function, the variable jawaban is 0. Here is the script

local jawaban
local b1 = script.parent.B1.B.Value
local b2 = script.parent.B2.B.Value
local oper = script.Parent.O:FindFirstChildWhichIsA("BoolValue")
local pilihan
local function calculate()
    if oper.Name == "x" then
        jawaban = b1 * b2
    elseif oper.Name == "+" then
        jawaban = b1 + b2
    elseif oper.Name == "-" then
        jawaban = b1 - b2
    end
    print(jawaban)
end
calculate()

the output will be

0

but when i change the b1 and b2. Like this

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()

the output wont be zero. Why this is happen?, btw this script is a local script

2 answers

Log in to vote
1
Answered by
imKirda 4491 Moderation Voter Community Moderator
3 years ago
Edited 3 years ago

Because in first example you are storing variable b1 and b2 as current value of the IntValue so the variable will never change by itself unless you change the variables.

In the second case you are typing b1.Value only when calling the function so it takes the newest value of the IntValue and uses that.

You can use both variants but in your case i see you can only use the second variant, otherwise it will always calculate the same value instead of new ones.

Ad
Log in to vote
1
Answered by
TGazza 1336 Moderation Voter
3 years ago

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
|----StarterPack
    \-----B1 -- Folder
        \-----B -- IntValue
    \-----B2 -- Folder
        \-----B -- IntValue
    \-----O -- Folder
        \-----x -- BoolValue
\---------LocalScript

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) -- you would need to check to see if the use isn't dividing by zero!
        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.

Answer this question