Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I make multiple scripts access the same dictionary?

Asked by 6 years ago

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

Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

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.

0
Awesome. This works perfectly Celt_ican 74 — 6y
Ad

Answer this question