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

How to fire a module script as a function from a script?

I didn't know how to say it as a question but I'll explain it here. So I am making a surface GUI with a customization feature to change the color of the surface GUI when clicked.

What I'm trying to accomplish is to make a function in a module script to avoid the pain of copying the script in each individual colors in case I have to make a change to the script.

So here's the module script:

-- The main Frame of the surface GUI to change the colors
program = script.Parent.Parent.Parent.Parent.Parent 

return function()
	program.Menu.Titlebar.BackgroundColor3 = script.Parent.BackgroundColor3
	program.Screen.Settings.Titlebar.BackgroundColor3 = script.Parent.BackgroundColor3
end

and here's the script that's in each button for the colors

local ColorChange = require(script.Parent.Parent.ColorChange)

script.Parent.MouseButton1Click:Connect(function()
	ColorChange()
end)

Its the first time I have tried to use ModuleScripts so help would be appreciated. I don't know if I have explained it properly so feel free to ask.

2 answers

✓ Accepted answer

Try this, I'm a bit rusty with ModuleScripts, but this should work IIRC Module Script

-- The main Frame of the surface GUI to change the colors
program = script.Parent.Parent.Parent.Parent.Parent 
local functions = {
ColorChange = function(framecolor)
    program.Menu.Titlebar.BackgroundColor3 = framecolor
    program.Screen.Settings.Titlebar.BackgroundColor3 = framecolor
end
}
return functions
end

Local Script:

local module = require(script.Parent.Parent.ColorChange)

script.Parent.MouseButton1Click:Connect(function()
    module.ColorChange(script.Parent.BackgroundColor3)
end)

4 comments

0

So it works for most part, the only thing is on the line 5 and 6 of the module scripts. What it was intended to do was to change the color of the frame to the color of the button it was clicked from.

0

Do you know how to find a solution to that?

0

@TheArtistTree updated the scripts, try that.

0

It works! Thanks ^^

You can do it like this

ModuleScript

local program = script.Parent.Parent.Parent.Parent.Parent

function module.ColorChange()
	program.Menu.Titlebar.BackgroundColor3 = script.Parent.BackgroundColor3
	program.Screen.Settings.Titlebar.BackgroundColor3 = script.Parent.BackgroundColor3
end

Script

local module = require(script.Parent.Parent.ColorChange)

script.Parent.MouseButton1Click:Connect(function()
    module.ColorChange()
end)

1 comment

0

Its unfortunately not a fix.

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