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

how do a variable from a function be known to another function or somehting ?

Asked by 4 years ago
Edited 4 years ago

here is my test script :

game.Workspace.test.Part2.ClickDetector.MouseClick:Connect(function(player)
    local a = 1
    local b = a + a
    Print(b)
end)

game.Workspace.test.Part1.ClickDetector.MouseClick:Connect(function(player)
    local c = b + b
    Print(c)
end)

somehow in the second function it is not recognizing the variable "b" even though i already put it on local on the first one

0
Local defines the variable as singular to its scope; it will only be understood in which block it is defined in. Unfortunately, you cannot create global variables within functions, so you’ll have to define b outside the function scopes. Ziffixture 6913 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

You could write the variable before the 2 function like this :

local a = 0
local b = 0
local c = 0
game.Workspace.test.Part2.ClickDetector.MouseClick:Connect(function(player)
    a = 1 -- Set a to 1
        b = a + a -- Set b to a+a
    Print(b)
end)

game.Workspace.test.Part1.ClickDetector.MouseClick:Connect(function(player)
    local c = b + b -- set c to b + b
    Print(c)
end)
0
I did your code , but I changed line 11 and put "Local c = c + b" I clicked part 2 , it printed "2" yes , I clicked part 1 , it printed "2" but "c" is supposed to update its value to 2 right , so if I clicked part1 again , it will print "4" but it did not , it still is two , can you tell me what I'm doing wrong ? rupertrosse 39 — 4y
0
Oops delete the local before the c and try again Nguyenlegiahung 1091 — 4y
0
Yep delete the local word before the c, i have tested it and it works 100% Nguyenlegiahung 1091 — 4y
Ad

Answer this question