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

How can I make multiple scripts access the same dictionary?

I'm creating imageLabels in a screenGui, and I want the imageLabels to animate in a certain way depending on the image loaded. 'If the requested image is Image1.png then set the height, width, number of frames, etc. to numbers according to the dictionary. I'd like to have something like this, but I'm not sure how the localScripts in the imageLabels would access the dictionary without copy pasting it into every script.

The dictionary would resemble this:

dictionary = {
	image1 = {
		height = 90,
		width = 100,
		frames = 16,
		image = "rbxgameasset://Images/image_name_1"
	},
	image2 = {
		height = 70,
		width = 120,
		frames = 4,
		image = "rbxgameasset://Images/image_name_2"
	},
}

1 answer

✓ Accepted answer

Module scripts!


A ModuleScript is a script that returns a single value. Often, this is a table. Any scripts can then require the module and retrieve that returned value.

-- module script

return {
    image1 = {
        height = 90,
        width = 100,
        frames = 16,
        image = "rbxgameasset://Images/image_name_1"
    },
    image2 = {
        height = 70,
        width = 120,
        frames = 4,
        image = "rbxgameasset://Images/image_name_2"
    },
}

And then any and all scripts can require this.

local dictionary = require(moduleScript)

print(dictionary.image1.frames) -- 16

This will work for local scripts and server scripts. If you don't want local scripts to be able to access the module, place the module in ServerStorage or ServerScriptService.

1 comment

0

Awesome. This works perfectly

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