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):
02 | local event = Instance.new( "RemoteEvent" ) |
03 | event.Name = "ItemClick" |
04 | event.Parent = game.Workspace |
05 | event.OnServerEvent:connect( function () |
06 | local ResourceManager = require(game.ReplicatedStorage.ResourceManager) |
07 | local gui = game.Players.LocalPlayer.PlayerGui.PickupItemGui.ImageLabel |
08 | for _,v in next ,game.Workspace.Items:GetChildren() do |
09 | local objectType = v.ObjectType.Value |
10 | local data = ResourceManager.Resources [ objectType ] |
11 | gui.TextLabel.Text = data.Name |
12 | if gui.Visible = = false then |
14 | gui.ResourceType.Value = v.ObjectType.Value |
ModuleScript it's pulling the data from:
02 | local ResourceManager = { } |
06 | Name = "Wooden Sword" , |
10 | Name = "Ancient Sword" , |
16 | ResourceManager.Resources = resources |
UPDATE:
I think I narrowed down the problem, and came up with a sort of solution. I created the following test script:
1 | local ResourceManager = require(game.ReplicatedStorage.ResourceManager) |
2 | for _,v in next ,game.Workspace.Items:GetChildren() do |
3 | local objectType = v.ObjectType.Value |
4 | local data = ResourceManager.Resources [ objectType ] |
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:
01 | print ( "ResourceManager Loaded" ) |
04 | local ResourceManager = { } |
09 | Name = "Wooden Sword" , |
14 | Name = "Ancient Sword" , |
19 | Name = "Health Potion" , |
25 | ResourceManager.Resources = resources |
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:
01 | local ResourceManager = require(game.ReplicatedStorage.ResourceManager) |
03 | for _,v in next ,game.Workspace.Items:GetChildren() do |
04 | v.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) |
LocalScript to handle GUI:
02 | local event = Instance.new( "RemoteEvent" ) |
03 | event.Name = "ItemClick" |
04 | event.Parent = game.Workspace |
05 | event.OnServerEvent:connect( function (item, arguments) |
06 | local ResourceManager = require(game.ReplicatedStorage.ResourceManager) |
07 | local gui = game.Players.LocalPlayer.PlayerGui.PickupItemGui.ImageLabel |
10 | gui.TextLabel.Text = data.Name |
11 | if gui.Visible = = false then |
13 | gui.ResourceType.Value = data.ID |
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.