Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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

1 comment

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.

1 answer

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)

3 comments

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 ?

0

Oops delete the local before the c and try again

0

Yep delete the local word before the c, i have tested it and it works 100%

This discussion is preserved as it was. New answers, comments, and votes are closed.