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

Why is this script indexing "nil" from my ModuleScript through a remote event?

Asked by
Morficc 36
8 years ago
Edited 8 years ago

I am trying to pull information from a ModuleScript through a remote event, and it is returning "Attempt to index local 'data' (a nil value).

This worked until I tried to put it into a RemoteEvent. Anyone have any tips?

Every model within Workspace/Items has a StringValue named ObjectType within it. When you click on an item, this remote event fires off, opens a GUI window (this works), and takes the Value of the ObjectType string, and sets the label of the GUI window to the "Name" field of that ObjectType within my Module Script. If that makes sense.

Script that is returning the error (Line 11):

01-- Remote Event Start
02local event = Instance.new("RemoteEvent")
03event.Name = "ItemClick"
04event.Parent = game.Workspace
05event.OnServerEvent:connect(function()
06local ResourceManager = require(game.ReplicatedStorage.ResourceManager)
07local gui = game.Players.LocalPlayer.PlayerGui.PickupItemGui.ImageLabel
08for _,v in next,game.Workspace.Items:GetChildren()do
09local objectType = v.ObjectType.Value
10local data = ResourceManager.Resources[objectType]
11gui.TextLabel.Text = data.Name
12if gui.Visible == false then
13gui.Visible = true
14gui.ResourceType.Value = v.ObjectType.Value
15else
16end
17end
18end)
19--Remote Event End

ModuleScript it's pulling the data from:

01--Resource Types
02local ResourceManager = {}
03local resources = {
04 
05    WoodSword = {
06                Name = "Wooden Sword",   
07                Type = "Slashing"
08    },
09    AncientSword = {
10        Name = "Ancient Sword",
11                Type = "Slashing"
12    }
13}
14 
15 
16ResourceManager.Resources = resources --allow other scripts to access this table
17return ResourceManager

UPDATE:

I think I narrowed down the problem, and came up with a sort of solution. I created the following test script:

1local ResourceManager = require(game.ReplicatedStorage.ResourceManager)
2for _,v in next,game.Workspace.Items:GetChildren()do
3        local objectType = v.ObjectType.Value
4        local data = ResourceManager.Resources[objectType]
5        print(data)
6end

My expectation was for it to pull my Object Types from the modulescript. (IE: WoodSword, and AncientSword). Instead the output was:

table: 0x6bbd34e0

Instead of pulling the names of my table entries, like I wanted, it was pulling "table: 0x6bbd34e0", which explains why my script wasn't accepting it. (The "nil" error in output, was because one of my items in Workspace/Items, was not in my ResourceManager modulescript, so it was returning as nil).

But my real problem was the table output. I am unsure as to how to return just "WoodSword" or "AncientSword". To fix this, I added an additional value to my table, ID. Under ID, I just replicated the name I wanted, like so:

01print("ResourceManager Loaded")
02 
03--Resource Types
04local ResourceManager = {}
05local resources = {
06 
07    WoodSword = {
08                ID = "WoodSword",
09                Name = "Wooden Sword",   
10                Type = "Slashing"
11    },
12    AncientSword = {
13                ID = "AncientSword",
14        Name = "Ancient Sword",
15                Type = "Slashing"
View all 26 lines...

Then I fixed my script by getting the value I wanted in my itemclick script, sending it through the RemoteEvent as an argument, then using this new ID value like so:

ItemClick Script:

01local ResourceManager = require(game.ReplicatedStorage.ResourceManager)
02 
03for _,v in next,game.Workspace.Items:GetChildren()do
04v.ClickDetector.MouseClick:connect(function(hit)
05    print('Click Detected')
06    local objectType = v.ObjectType.Value
07    local data = ResourceManager.Resources[objectType]
08    game.Workspace.ItemClick:FireServer(data)
09end)   
10end

LocalScript to handle GUI:

01-- Remote Event Start
02local event = Instance.new("RemoteEvent")
03event.Name = "ItemClick"
04event.Parent = game.Workspace
05event.OnServerEvent:connect(function(item, arguments)
06local ResourceManager = require(game.ReplicatedStorage.ResourceManager)
07local gui = game.Players.LocalPlayer.PlayerGui.PickupItemGui.ImageLabel
08local data = arguments
09print(arguments)
10gui.TextLabel.Text = data.Name
11if gui.Visible == false then
12gui.Visible = true
13gui.ResourceType.Value = data.ID
14else
15end
16end)

At the present time, this solutions does work from me. So you could mark this as answered. But I would be very interested still, to hear why my original method was not returning the values I wanted, or a better way to accomplish what I am doing.

Thank you all.

Answer this question