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

How to share variables between scripts and change them?

Hey guys, i'm new to scripting in Roblox Studio, so I hope you could help me with a problem... So, i have a ModuleScript and i have a variable called "Buttonleft" in it. My idea is to crate a game that, when 4 buttons are pressed (not necessarily at the same time), a door is opened (like Dead by Daylight and the generators)... My problem is, when i press a button, the variable Buttonleft (wich is a int variable equal to 0) increases the value, becoming Buttonleft = 1. So, i need to everytime a player presses a button, this variable increases, but it is not working. The ModuleScript keep saying that Buttonleft = 1 when i press more than 1 button, when it should be printing "Buttonleft = 2" "Buttonleft = 3" and on and on... But it just doesn't work :/ Here's my codes:

ModuleScript

local Buttons = { Buttonleft = 0 }
return Buttons

Buttons Script

clickquantity = 5
Buttons = require(game.ServerScriptService.Buttons)

function clickbutton()
	workspace.Painel2.SurfaceGui.TextLabel.Text = clickquantity .. " Click's left"
	clickquantity = clickquantity - 1
	if clickquantity == 0 then
		workspace.Painel2.SurfaceGui.TextLabel.Text = "Finished Button"
		Buttleft = Buttons.Buttonleft
		Buttleft = Buttleft + 1
		print(Buttleft)
	elseif clickquantity < 0 then
		workspace.Painel2.SurfaceGui.TextLabel.Text = "Finished Button"
	end 
end

game.Workspace.Button2.ClickDetector.MouseClick:Connect(clickbutton)

Can you please help me? Thanks :)

1 comment

0

You could use values.

2 answers

✓ Accepted answer

Every script has its own copy of all variables and functions (even the ones that Lua considers "global".) So the information in variable Buttleft won't be able to get out of that one script.

One exception is that require always returns the same object if used more than once in the same server or same client. So you could update the Buttons.Buttonleft value actually inside the module table:

    Buttons.Buttonleft = Buttons.Buttonleft + 1
    print(Buttons.Buttonleft)

That might work for some limited uses, but you still have separate values on the server and on every player's client if any LocalScript uses the ModuleScript. And it won't get saved on a server restart. I would consider using an IntValue or a value in a GlobalDataStore instead.

It would appear you messed up pass by value and pass by reference. On line 9 and 10, tables will pass by reference but most primitive types will not. You must write out Buttons.Buttonleft = Buttons.Buttonleft + 1

As primitive values will copy instead of just passing the location address. You can play around with this a bit in some functional environments:


function a(someTable)
	print(someTable)
end

local myTable = {}
print(myTable)
a(myTable)

--Both print the same thing.

function b(someNumber)
	someNumber = someNumber+1
	print(someNumber)
end

local myNumber = 4
print(myNumber+1) -->5
b(myNumber)-->5
print(myNumber)-->4

Similarly these rules are the same for the contents of a table.

function c(somethingInATable)
	--I'm going to assume somethingInATable is a number which is bad practice
	--but this is demonstration.
	somethingInATable = somethingInATable+1
	print(somethingInATable)
end

local myTable2 = {a = 1,b = 2,c = 3,d = 4}

c(myTable2.d)-->5
print(myTable2.d)-->4

1 comment

0

Ok, i think i see your point, thank you for the tips to play around :)

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