game.ReplicatedStorage.Remotes.Load_Inventory.OnClientEvent:Connect(function(xx)
for _, x in pairs(xx) do
print(x)
end
end)
server
local inv = {"Test"}
game.Players.PlayerAdded:Connect(function(pl)
function reloadInventory()
game.ReplicatedStorage.Remotes.Load_Inventory:FireClient(pl, inv)
end
end)
error it returns is
20:35:24.703 Players.SpookyHexcode.PlayerGui.ScreenGui.Frame.LocalScript:4: invalid argument #1 to 'pairs' (table expected, got nil) - Client - LocalScript:4
20:35:24.703 Stack Begin - Studio
20:35:24.703 Script 'Players.SpookyHexcode.PlayerGui.ScreenGui.Frame.LocalScript', Line 4 - Studio - LocalScript:4
20:35:24.703 Stack End - Studio
By making the function local, I found a way to make it work.
Server:
local inv = {"Test"}
function reloadInventory(plr)
game.ReplicatedStorage:WaitForChild("Remotes").Load_Inventory:FireClient(plr, inv)
end
game.Players.PlayerAdded:Connect(function(pl)
reloadInventory(pl)
end)
I’m not sure why you’re creating a function inside of an event in this case, so I just created a new function outside of the event and gave it a parameter plr which you can define in the PlayerAdded event
Creating a function for the sole purpose of firing a remote event may take up more memory than you really need. If you just fire the remote event like so:
local Datastore = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local Replicated = game:GetService("ReplicatedStorage")
local HttpService = game:GetService("HttpService")
local items = require(game.ReplicatedStorage.Items)
local inventorydata = Datastore:GetDataStore("inventorydata")
local inv = {}
game.Players.PlayerAdded:Connect(function(pl)
game.ReplicatedStorage:WaitForChild("Remotes").Load_Inventory:FireClient(pl, inv)
end)
Players.PlayerAdded:Connect(function(pl)
local info
pcall(function()
info = inventorydata:GetAsync(pl.UserId)
print(pl.UserId)
print("LOG: Loading " .. pl.Name .. "'s data was loaded successfully")
for _, x in pairs(inv) do
local Backpack = pl.Backpack
if Backpack:FindFirstChild(x) then
else
local b = items.Knives[x][1]:Clone()
b.Parent = Backpack
end
end
end)
if info then
local decode = HttpService:JSONDecode(info)
inv = decode
end
for _, x in pairs(inv) do
local Backpack = pl.Backpack
local b = items.Knives[x][1]:Clone()
b.Parent = Backpack
end
pcall(function()
info = inventorydata:GetAsync(pl.UserId)
end)
game.ReplicatedStorage.Remotes.GiveKnife.OnServerEvent:Connect(function(text)
table.insert(inv, text)
game.ReplicatedStorage:WaitForChild("Remotes").Load_Inventory:FireClient(pl, inv)
end)
end)
Players.PlayerRemoving:Connect(function(pl)
local encode = HttpService:JSONEncode(inv)
inventorydata:SetAsync(pl.UserId, encode)
encode = HttpService:JSONEncode(inv)
inventorydata:SetAsync(pl.UserId .. "INV", encode)
print("LOG: Loading " .. pl.Name .. "'s data was saved successfully")
end)
whole server script
i have two players added to see if there was a issue with that
Also, when you’re decoding JSON and setting env = decode, are you guaranteed that info is for sure valid JSON? I guess you’d just get an error otherwise though.
Also, it might make sense just to use a RemoteFunction instead of a RemoteEvent, since you need to give info back to the caller.