Hello, I'm trying to access this dictionary through a remote event, wondering why it's producing , 'attempt to index local 'item' a nil value
---LOCALSCRIPT---
script.Parent.MouseEnter:Connect(function()
local item = game.ReplicatedStorage.Remotes.FindItem:FireServer(script.Parent.Name)
desc.Text = item[3]
atk.Text = item[1]
def = item[2]
end)
--- SCRIPT---
ITEMS = {
[“Basic Sword”] = {1,20,0,“The basic sword, almost too basic…”,3,“none”} ,
[“Basic Shield”] = {1,0,15,“Well it’s a shield… so yea”,3,“none”} ,
[“The Essence”] = {100,150000,50000,“The essence of the creator, spread through the deepest levels of the dungeon.”,500,“Essence”} ,
}
game.ReplicatedStorage.Remotes.FindItem.OnServerEvent:Connect(function(plr,name)
return ITEMS[name]
end)
Right now, the server script is returning the table to itself. In order for the client to receive the item table, you must use RemoteEvent:FireClient().
Client:
1 | script.Parent.MouseEnter:Connect( function () |
2 | game.ReplicatedStorage.Remotes.FindItem:FireServer(script.Parent.Name) |
3 | local item |
4 | game.ReplicatedStorage.Remotes.FindItem.OnClientEvent:Connect( function (item_) |
5 | item = item_ |
6 | end ) |
7 | desc.Text = item [ 3 ] |
8 | atk.Text = item [ 1 ] |
9 | def = item [ 2 ] |
end)
Server:
1 | ITEMS = { |
2 | [ "Basic Sword" ] = { 1 , 20 , 0 , "The basic sword, almost too basic…" , 3 , "none" } , |
3 | [ "Basic Shield" ] = { 1 , 0 , 15 , "Well it’s a shield… so yea" , 3 , "none" } , |
4 | [ "The Essence" ] = { 100 , 150000 , 50000 , "The essence of the creator, spread through the deepest levels of the dungeon." , 500 , "Essence" } |
5 | } |
6 | game.ReplicatedStorage.Remotes.FindItem.OnServerEvent:Connect( function (plr,name) |
7 | game.ReplicatedStorage.Remotes.FindItem:FireClient(plr,ITEMS [ name ] ) |
8 | end ) |
If you’re confused about :FireClient or .OnClientEvent, here’s the documentation on the wiki.
Instead of using a RemoteEvent, use a RemoteFunction instead and it should start working.
https://developer.roblox.com/api-reference/class/RemoteFunction