Remote Event Broken?

Heres my script it returns this error

LocalScript

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

i’m sending a table

if i add player infront of xx in local script it returns that error if i remove it, it prints the players name

Is anything modifying inv in your server script?

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

just for easier use, i have a remote that adds items too

FireClient: player argument must be a Player object

table.insert and i’m just inserting a item ( string )

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:

game.Players.PlayerAdded:Connect(function(pl)
	game.ReplicatedStorage:WaitForChild("Remotes").Load_Inventory:FireClient(pl, inv)
end)

This will give you the same outcome without taking up any unnecessary memory usage.

Now it just prints me SpookyHexcode.

Does your OnClientEvent event in the local script have more than one parameter? It should only have 1 in this case

just one parameter, TABLE. It doesn’t have any more parameters

I think we need to see more of your code to understand what’s going on and to see if any side-effects are causing a problem.

1 Second.


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

Here’s some stuff:

When you’re connecting to GiveKnife on the server, the first argument is the player, not text. Should be changed to:

game.ReplicatedStorage.Remotes.GiveKnife.OnServerEvent:Connect(function(player, text)
	...
end)

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.

1 Like

i have no error, its my first time using json to save data too

You fixed it thank you very much

1 Like