Trying to learn modules very confused please help

im trying to make my part randomly change colors but not just to any color i want it to pick from my table of numbers, heres my code

this is my module script

local colorsmodule = {
	Red = "255, 0, 0",
	Blue = "0, 0, 255",
	Green = "0, 255, 0",
	Yellow = "255, 255, 0",
	Cyan = "0, 255, 255",
	Pink = "255, 0, 255"
}
return colorsmodule

this is my script,

Rainbow = workspace.RainbowPart
module = game.Workspace.ModuleScript


Rainbow.Touched:Connect(function(changecolor)
	Rainbow.CanTouch = false
	Rainbow.BrickColor = BrickColor.random(module)
	wait(0.25)
	Rainbow.CanTouch = true
end)
6 Likes

if you want to make something rainbow i would recommend to use tweenservice instead.

5 Likes

its not about the function im juist messing around with that, i want to learn how to use module scripts

3 Likes

You need to require a module by doing module = require(game.Workspace.ModuleScript). You also shouldnt have module scripts in workspace, its better to just put them in ServerScriptService. Another thing to note is that its best to make your variables local by doing: local module = gam.. and local rainbow = worksp...

4 Likes

well to request an informations from a module script you need to use require(modulepath) and if you want something specific of that module you can do require(modulepath).wantedvariable information
example require(game.ReplicatedStorage.GameConfig).Level so you will only get the information of the level and nothing else

3 Likes

how do i actually change the brickcolor?

	rainbow.BrickColor = BrickColor.Random(module)

this code just makes the brick change to any random color not the ones in the list

4 Likes
local Rainbow = workspace.RainbowPart
local Moduele = require(game.Workspace.ModuleScript)

Rainbow.Touched:Connect(function(changecolor)
    Rainbow.CanTouch = false
    local colorNames = {}
    for colorName, _ in pairs(Module) do
        table.insert(colorNames, colorName)
    end
    local randomColorName = colorNames[math.random(1, #colorNames)]
    Rainbow.BrickColor = BrickColor.new(Module[randomColorName])
    wait(0.25)
    Rainbow.CanTouch = true
end)
2 Likes

module:

local Red = Color3.fromRGB(255, 0, 0)
local Blue = Color3.fromRGB(0, 0, 255)
local Green = Color3.fromRGB(0, 255, 0)
local Yellow = Color3.fromRGB(255, 255, 0)
local Cyan = Color3.fromRGB(0, 255, 255)
local Pink = Color3.fromRGB(255, 0, 255)

local colorsmodule = {
	Red,
	Blue,
	Green,
	Cyan,
	Pink
}

return colorsmodule

script:

local Rainbow = workspace.RainbowPart
local module = require(game.Workspace.ModuleScript)

Rainbow.Touched:Connect(function()
	Rainbow.CanTouch = false
	local random_number = math.random(1,#module)
	Rainbow.BrickColor = BrickColor.new(module[random_number])
	wait(0.25)
	Rainbow.CanTouch = true
end)
2 Likes

Your doing it wrong:

local colorsmodule = {}
colormodule.colors = {
	Red = Color3.fromRGB(255, 0, 0),
	Blue = Color3.fromRGB(0, 0, 255),
	Green = Color3.fromRGB(0, 255, 0),
	Yellow = Color3.fromRGB(255, 255, 0),
	Cyan = Color3.fromRGB(0, 255, 255),
	Pink = Color3.fromRGB(255, 0, 255)
}
return colorsmodule
Rainbow = workspace.RainbowPart
local colormodule = require(workspace.ModuleScript)

Rainbow.Touched:Connect(function(changecolor)
	Rainbow.CanTouch = false
	Rainbow.Color = colormodule.color[math.random(1,#colormodule.color)]
	wait(0.25)
	Rainbow.CanTouch = true
end)
2 Likes

just a script storage that you can make functions in and use them from another script.

Module recieves information for a function
Continues to do what its meant to do with it. (you say what it does)

script just watches as the module does it

1 Like

Youā€™re doing it wrong.

local Colors = {
	{ 255, 0, 0 },
	{ 0, 0, 255 },
	{ 0, 255, 0 },
	{ 255, 255, 0 },
	{ 0, 255, 255 },
	{ 255, 0, 255 },
}

local Rainbow = assert(workspace:WaitForChild("RainbowPart", 5))

Rainbow.Touched:Connect(function(otherPart)
	if not otherPart.Parent:FindFirstChildOfClass("Humanoid") then
		return
	end

	Rainbow.CanTouch = false
	Rainbow.Color = Color3.fromRGB(unpack(Colors[math.random(#Colors)]))

	task.wait(0.25)

	Rainbow.CanTouch = true
end)
2 Likes

Both solutions give the same answer I dont understand what you are trying to achieve here?

1 Like

Your solution errors, mine doesnā€™t.

#checkmate

2 Likes

So? I donā€™t have time for your nonsense. Please be professional. Also technically your making it more complex for OP. Looks like OP is a simple minded folk so:

local Colors = {
	Color3.fromRGB(255, 0, 0),
	Color3.fromRGB(0, 0, 255),
	Color3.fromRGB(0, 255, 0),
	Color3.fromRGB(255, 255, 0),
	Color3.fromRGB(0, 255, 255),
	Color3.fromRGB(255, 0, 255)
}

local Rainbow = assert(workspace:WaitForChild("RainbowPart", 5))

Rainbow.Touched:Connect(function(otherPart)
	if not otherPart.Parent:FindFirstChildOfClass("Humanoid") then
		return
	end

	Rainbow.CanTouch = false
	Rainbow.Color = Colors[math.random(#Colors)]

	task.wait(0.25)

	Rainbow.CanTouch = true
end)

Post your ā€œjokesā€ on discord or something or perhaps in the DMS.

1 Like

Your solution yet again errors.

3 Likes

Oh really? Does it?

1 Like

Ho there, thereā€™s a few ways of going about this, but Iā€™ll go over one way. In modules, you can add functions that do whatever you want, and then you can call them outside of the ModuleScript. So I recommend adding a RandomColor function inside the module script which goes through your colors and arbitrarily choose one. Hereā€™s how Iā€™d go about it.

local colorsmodule = {
} -- global table for module

local Colors = {	-- color table
	Red = "255, 0, 0",
	Blue = "0, 0, 255",
	Green = "0, 255, 0",
	Yellow = "255, 255, 0",
	Cyan = "0, 255, 255",
	Pink = "255, 0, 255"
}

local function CountDict(Dict) -- Dictionary counting since roblox doesn't natively support it
	local count = 0
	
	for Index,Value in pairs(Dict) do
		count += 1
	end
	
	return count
end

function colorsmodule.RandomColor() -- randomizes color based on dictionary size, then loop through the dictionary and figure out which index to stop at!
	local DictSize = CountDict(Colors)
	local RandomizedNumber = math.random(1,DictSize)
	local Count = 0
	local ChosenColor = nil
	
	for Color,Value in pairs(Colors) do
		Count+=1
		
		if RandomizedNumber == Count then
			ChosenColor = Value
			break
		end
	end
end


return colorsmodule

You can leave the server script as is and it should work fine, but please try to learn from this and ask if you need help!


Edit: Changed the gif to an actual video

2 Likes

Why use a module here when you can just create a table with colors?

local Rainbow = workspace.RainbowPart

local colorsmodule = {
	Color3.fromRGB(255, 0, 0), -- Red
	Color3.fromRGB(28, 58, 255), -- Blue
	Color3.fromRGB(66, 255, 52) --Green
}

Rainbow.Touched:Connect(function(plr) -- plr who touched
	Rainbow.CanTouch = false
	local Random_Color = math.random(1, #colorsmodule)
	Rainbow.Color = colorsmodule[Random_Color]
	task.wait(0.25)
	Rainbow.CanTouch = true
end)
2 Likes

module script:

local colorsmodule = {
	Color3.fromRGB(255, 0, 0),
	Color3.fromRGB(0, 0, 255),
	Color3.fromRGB(0, 255, 0),
    Color3.fromRGB(255, 255, 0),
	Color3.fromRGB(0, 255, 255),
	Color3.fromRGB(255, 0, 255),
}

return colorsmodule

script:

Rainbow = workspace.RainbowPart
local colors = require(workspace.ModuleScript)

task.spawn(function()
   while task.wait(0.25) do
      Rainbow.Color = colors[Random.new():NextInteger(1, #colors)]	
   end
end)

basically, you should do Color3.fromRGB() when using rgb colours.
Brickcolor.random() picks a random colour from Brickcolor.palette().

module scripts cannot run on their own.
for them to run, they must be called by another script using require().
also, they MUST return something. In this case, a table.

This table includes the colours we set.

You do not need a .Touched event to change its colours forever - what if it is in the air? just use a while loop.

task.spawn is a bit advanced so I wonā€™t explain it.

wait() is deprecated. meaning it is no longer supported. it has been replaced by task.wait(). use that instead

Random.new():NextInteger() ensures that have a truly random number. If you didnā€™t know, roblox doesnt have random numbers, but generated lines of numbers called ā€˜seedsā€™. Random.new() with no parameters, the things you put in the (), generates a random seed.

Links:
Brickcolor.palette()
Brickcolor.random()
Color3.fromRGB()
Random.new()
:NextInteger()
task.wait()
task.spawn()
while loops
.Touched
require()
module script

3 Likes

Unfortunately your solution wouldnā€™t work since the OP and even you used a dictionary to list the colors, which comes with two downsides.

1 - You canā€™t index them via numbers if they are indexed with strings (unless already indexed as numbers)
2 - You canā€™t natively get the size of a dictionary.

Iā€™ve solved those problems in my reply to the OP. However I did like your explanation on module scripts. Also I donā€™t really like how youā€™re generation a new seed each time a color needs to be set, it would honestly suffice if you used one pre-generated seed at the start of the script using Random.new(). All in all, if the dictionary was a normal table, your solution would almost suffice and if you didnā€™t use the BrickColor property but instead used Color3, it would be much cleare.

I donā€™t mean any hate, Iā€™m just providing constructive criticism to avoid any confusion to others.

Edit: please say if Iā€™ve said something wrong too, thanks!

1 Like