Global functions

So I made a mess. But its working.
image
every frame have the same code.

local point = script.Parent
point.BackgroundColor3 = Color3.new(0, 0, 1)
while true do
	if _G.Dotcol == 0 then
		point.BackgroundColor3 = Color3.new(0, 1, 0)
	elseif _G.Dotcol == 1 then
		point.BackgroundColor3 = Color3.new(0, 0, 1)
	elseif _G.Dotcol == 2 then
		point.BackgroundColor3 = Color3.new(1, 0, 0)
	elseif _G.Dotcol == 3 then
		point.BackgroundColor3 = Color3.new(0, 0, 0)
	end
	wait(0.05)
end

I made so many frames to make circle from them.
Image label doesn’t want to react to this code.
Are there a global functions in lua? so i can make one function for all frames, and work on one code, not on their own. I’m begginer scripter and I didin’t found any tutorial on how to make a global function.

You can just make one localscript and go through all frames at once and you can just make a table of colours instead of that if-else chain:

local ColorTable = {
	[0] = Color3.new(0, 1, 0),
	[1] = Color3.new(0, 0, 1),
	[2] = Color3.new(1, 0, 0),
	[3] = Color3.new(0, 0, 0)
}
local Dots = script.Parent:WaitForChild("dots"):GetChildren() --:GetChildren() is used to get the chlidren of an instance. In this case it's used for getting all frames in the folder as a table.

while true do
	for Index, Value in pairs(Dots) do --Iterates through the frames.
		Value.BackgroundColor3 = ColorTable[_G.Dotcol] or Value.BackgroundColor3 --Gets the colour attached to same number as _G.Dotcol's value, if there is none hen it will use the old one.
	end
	wait(0.05)
end

image

1 Like

image
that code isn’t working ;-;

Why, what error is it throwing?

Players.kupakarakana.PlayerGui.dot.LocalScript:11: invalid argument #3 (Color3 expected, got nil)

Oh, you didn’t even give a value to _G.Dotcol. If that will be the case in most times then change this line:

Value.BackgroundColor3 = ColorTable[_G.Dotcol]

to this:

Value.BackgroundColor3 = ColorTable[_G.Dotcol] or Value.BackgroundColor3

So when ColorTable[_G.Dotcol] returns nil background colour will stay the same.

1 Like

oke, I changed _G.Dotcol to have value from beggining. It didin’t had it from start of game. Problem solved. Thank you for your great work.