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
7 years ago
Edited 7 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):

-- Remote Event Start
local event = Instance.new("RemoteEvent")
event.Name = "ItemClick"
event.Parent = game.Workspace
event.OnServerEvent:connect(function()
local ResourceManager = require(game.ReplicatedStorage.ResourceManager)
local gui = game.Players.LocalPlayer.PlayerGui.PickupItemGui.ImageLabel 
for _,v in next,game.Workspace.Items:GetChildren()do
local objectType = v.ObjectType.Value
local data = ResourceManager.Resources[objectType]
gui.TextLabel.Text = data.Name
if gui.Visible == false then
gui.Visible = true
gui.ResourceType.Value = v.ObjectType.Value
else
end
end
end)
--Remote Event End

ModuleScript it's pulling the data from:

--Resource Types
local ResourceManager = {}
local resources = {

    WoodSword = {
                Name = "Wooden Sword",    
                Type = "Slashing"
    },
    AncientSword = {
        Name = "Ancient Sword",
                Type = "Slashing"
    }
}


ResourceManager.Resources = resources --allow other scripts to access this table
return ResourceManager

UPDATE:

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

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

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:

print("ResourceManager Loaded")

--Resource Types
local ResourceManager = {}
local resources = {

    WoodSword = {
                ID = "WoodSword",
                Name = "Wooden Sword",    
                Type = "Slashing"
    },
    AncientSword = {
                ID = "AncientSword",
        Name = "Ancient Sword",
                Type = "Slashing"
    },
        Potion = {
                ID = "Potion",
                Name = "Health Potion",
                Type = "Item"
        }
}


ResourceManager.Resources = resources --allow other scripts to access this table
return ResourceManager


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:


local ResourceManager = require(game.ReplicatedStorage.ResourceManager) for _,v in next,game.Workspace.Items:GetChildren()do v.ClickDetector.MouseClick:connect(function(hit) print('Click Detected') local objectType = v.ObjectType.Value local data = ResourceManager.Resources[objectType] game.Workspace.ItemClick:FireServer(data) end) end

LocalScript to handle GUI:

-- Remote Event Start
local event = Instance.new("RemoteEvent")
event.Name = "ItemClick"
event.Parent = game.Workspace
event.OnServerEvent:connect(function(item, arguments)
local ResourceManager = require(game.ReplicatedStorage.ResourceManager)
local gui = game.Players.LocalPlayer.PlayerGui.PickupItemGui.ImageLabel 
local data = arguments
print(arguments)
gui.TextLabel.Text = data.Name
if gui.Visible == false then
gui.Visible = true
gui.ResourceType.Value = data.ID
else
end
end)

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