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

How do I get multiple data from an array that is within a ModuleScript to another separate script?

Context

This is my second day of using ModuleScripts, so I have not fully understood the concepts of ModuleScripts, but I know what ModuleScripts are used for and how to use them in basic situations.

I have a ModuleScript and a Script in ServerScriptService, a LocalScript and a Frame with a bunch of TextLabels in a ScreenGui which is in a StarterGui, and a RemoteEvent located in ReplicatedStorage.

I also have a folder that duplicates itself to a player when they join the game. The original location of this folder is in ServerStorage. I'll refer this as the PlayerFolder. The PlayerFolder has a bunch of values and within each value has another bunch of values.

Desired Process

What I wanted this whole process to do is the following (in chronological order): -Pressing [K] will fire Function A from the LocalScript. -Upon firing Function A, it will fire a RemoteEvent to the server. -The Script will recognize the RemoteEvent being fired and will fire Function B. -Upon firing Function B, the values from the PlayerFolder will be replaced with their respective data from the specified array within the ModuleScript. -The TextLabels will change their display text based off the values in the PlayerFolder.

The problem is when I set a variable to locate one of the arrays within the ModuleScript, the value of the location is set to nil. Here's what I have:

-- LocalScript in ScreenGui

local RemoteEvent = game:GetService("ReplicatedStorage").RemoteEvent

function A()
	RemoteEvent:FireServer()
	--additional irrelevant code
end

function OnKeyPress(InputObject, GameProcessedEvent)
	if InputObject.KeyCode == K then
		Function A()
		--make gui visible code
	end
end

game:GetService("UserInputService").InputBegan:connect(OnKeyPress)

==========

-- Script in ServerScriptService

local RemoteEvent = ReplicatedStorage.RemoteEvent
local ModuleScript = require(game:GetService("ServerScriptService").ModuleScript)

function Function B(player)

	local PlayerFolder = player.PlayerFolder
	local Value01 = PlayerFolder["01"] --mouse
	local Value02 = PlayerFolder["02"] --cat
	local Value03 = PlayerFolder["03"] --owner
	
	local Data01 = ModuleScript[Value01.Value]
	local Data02 = ModuleScript[Value02.Value]
	local Data03 = ModuleScript[Value03.Value]
	
	--Slot 1
	Value01.Value = Data01
	Value01.ValueName.Value = Data01[1]
	Value01.ValueType.Value = Data01[2]
	--Slot 2
	Value02.Value = Data02
	Value02.ValueName.Value = Data02[1]
	Value02.ValueType.Value = Data02[2]
	--Slot 3
	Value03.Value = Data03
	Value03.ValueName.Value = Data03[1]
	Value03.ValueType.Value = Data03[2]

end

RemoteEvent.OnServerEvent:Connect(B)

==========

-- ModuleScript in ServerScriptService

local module = {

	mouse = {"the mouse", "25"};
	cat = {"sad cat", "52"};
	owner = {"mr owner", "100"};

}

return module

Error in output:

11:25:56.445 - ServerScriptService.Script:19: attempt to index local 'Data01' (a nil value)

May anyone let me know what I've done wrong? I'll be happy to provide more information upon request.

2 comments

0

What is Inventory 01?

0

My apologies mau, I didn't realize Inventory01 was still there and I forgot to change it to Data01

1 answer

I solved my own question! The code wasn't the problem, I spelt the name of a value wrong. Correcting it fixed everything! Please use the code for reference if necessary for your own games!

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