Help with getting data from module scripts

Hey there, I am currently working on a fishing game as a small project to touch up on my developing skills and was having some trouble with getting data from a module script.

My goal is to clone a template gui I have created and change a TextLabel inside it for each fish in my table.

Problem:
The problem is that I cannot get what fish are in what FishingArea out of the table.

More info:
I have a value inside the player that knows what fishing area that they are in and I need to reley that info to the script so that the table knows what fishing area to get the info about the fish.

Script 1: (Module script)

local FishSpots = {
	
	["Ocean"] = {
		IndexPos = 1;
		Backup = "Carp";
		Rarities = {
			["Silver Carp"] = {80, 1};
			["Devils Hole Pupfish"] = {20,2};
			
			}
		};
		["River"] = {
			IndexPos = 2;
			Backup = "Carp";
			Rarities = {
				["Silver Carp"] = {60, 1};
				["Devils Hole Pupfish"] = {40,2};
			}
	}
}

return FishSpots

Script 2: (Local script inside the gui)

local plr = game.Players.LocalPlayer
local template = script.Parent.Template
local FishSpot = plr.Values.FishArea
local FishSpots = require(plr.PlayerScripts:WaitForChild("FishSpotStats"))
print(FishSpots)


for i, v in pairs(FishSpots) do
	--obviously some code needs to go in here 
	template:Clone()
end

Any help is very much apricated!!

2 Likes

First, make it a function to retrieve the info. For example, try doing:

fuction (Module Script Name).RetreiveInfo()
     return FishSpots --the table you want to use
end

Then for the local script, do:

local ModuleScript = require(plr.PlayerScripts:WaitForChild("FishSpotStats"))
local FishSpots = ModuleScript.RetreiveInfo()
--then the rest of your code

In here, you are doing a for loop trought your module table, im not sure to see the issue here, you just need to use the V variable to get the value in the for loop

Thanks for the tip, Ill go do that know but do you know how I could use the FishArea value inside the player to to get what the fish out of the table for the fish area that they are in?

You don’t need a function to get a module table if the whole module is a table, you don’t need it since the table is already returned by default. If you want to get a part of the table by using the name of the area where the player is you could do something like this

local areaOfThePlayer = something here that contian the name of the area
local yourModule = require(somethingHere)
local areaOfThePlayer = yourModule[areaOfThePlayer] -- will return you the part of the table matching the name of the area

Yeah there’s no issue so far, I just need help moving forward, I’ve never really used module scripts before so I’m kinda confused on how I can get the FishArea then the fish that are in it.

Oh thank you so much, I’ll go try that out right now.

local areaOfThePlayer = "Ocean"
local yourModule = require(module.path.here)
local areaOfThePlayer = yourModule[areaOfThePlayer] -- will return you the part of the table matching the name of the area

for i,v in pairs(areaOfThePlayer) do
 -- here all the value inside Ocean will be looped, including the table Rarities, note however that you will need another loop or another ["name here"] to acess these
end
1 Like

Thanks so much, I understand that part of it now :))
But just one more thing, if I was wanting to print the the name of each fish and the chance (which is the value after it in the table) how would I go about doing that?

Sorry to be a pain.

local test = {
	["value"] = {
		["Silver Carp"] = {60, 1},
		["Devils Hole Pupfish"] = {40,2}
	}
}

for keyName,keyValue in pairs(test.value) do
	print(keyName) -- will print the string name or int index of there is no name
	print(keyValue) -- will print the value of the key, in this case, a table since we have 60 at the index [1] and 1 at the index [2] (Only reffering to the first value index [1] and not the [2] index)
end
1 Like

That code works great but is it possible to do that with a module script? or will the table have to be in the same script?

Yes it is the same, in the exemple my table is test in the script, but you can also use your table by requiring the module script, if you want to run the exact thing as my exemple but in your module you would need to edit pairs(test.value) to pairs(FishSpots["Ocean"].Rarities)

The for loop is basicaly “looping” trought all value in a table

1 Like

If you want to create an immutable copy of data, just use table.freeze and table.clone

Awesome! I got it working :)) Thank you for being so helpful.
I just have 1 final question before I go.
If I want to clone a template frame for each of the values would I just add a

local clone = template:Clone()
clone.TextLabel.Text = keyName

Yes it should work for specific value but if you do it in a for loop it will create one for IndexPos, Backup, and then Rarities witch will probably throw a error since you can’t set a table as a string (textlabel.text).
Also don’t forget to set your clone parent a the end othewise he won’t show up

If the case is solved don’t forget to press the green button to tell others

1 Like

Thank you so much for all your help! I wish there was more that I could do then just give you a solution. You’ve been so helpful and understanding.

1 Like