Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Is it possible to transfer table information with remote events?

I'm trying to transfer some information from a server script into a client script using remote events. And i'm transfering some table information from the server script into the client so hackers wouln't be able to access the code.

I can't seem to get it to work it just prints out an error in the outpot.

Here's my server script code:

local inv = {nil, nil, nil, nil, nil, nil, nil, nil, nil, nil}

local nextSlot = 1
local currentSlot = 1

function ToolHotbar(slot, empty, item)
	RemoteEvents.ToolHotbar:FireClient(slot, empty, item, inv[slot])
end

local function checkIfEmpty(empty, item)
	if inv[nextSlot] ~= nil then 
		nextSlot = nextSlot + 1
		currentSlot = currentSlot + 1
		if inv[nextSlot] ~= nil then
			nextSlot = nextSlot + 1
		end
		empty = true
		if nextSlot <= 10 then
			ToolHotbar(nextSlot, empty, item)
		else
			print("Your inventory is full please empty it.")
		end
	else
		print("Slot"..nextSlot.. " Is empty, Adding item...")
		empty = true
		ToolHotbar(nextSlot, empty, item)
	end
end

function addItem(empty, item)
	checkIfEmpty(empty, item)
end

addItem(nil, "Dirt")

Client script:

local rep = game.ReplicatedStorage
	local items = rep.items
	local Events = rep.Events;
	local RemoteEvents = Events.RemoteEvents;
	
local gui = script.Parent:WaitForChild("ScreenGui")
	local Toolbar = gui.Toolbar

local nextSlot = 1
local currentSlot = 1

RemoteEvents.ToolHotbar.OnClientEvent:connect(function(slot, empty, item, inv)
	
	if (type(inv) ~= "table") then
		inv = {inv}
	end	
	
	if empty then 
		local imageID = items[item]:FindFirstChild("imageID").Value
		Toolbar["Slot"..nextSlot].itemImage.Image = "rbxassetid://"..imageID
		Toolbar["Slot"..nextSlot].itemImage.Visible = true
		inv[nextSlot] = item
	end
end)

Error output:

20:49:29.370 - Unable to cast value to Object
20:49:29.371 - Stack Begin
20:49:29.371 - Script 'ServerScriptService.MainScript', Line 15 - global ToolHotbar
20:49:29.371 - Script 'ServerScriptService.MainScript', Line 35 - upvalue checkIfEmpty
20:49:29.372 - Script 'ServerScriptService.MainScript', Line 40 - global addItem
20:49:29.372 - Script 'ServerScriptService.MainScript', Line 43
20:49:29.373 - Stack End

1 comment

0

The error output refers to lines a script (or lines in your script) that you haven't posted

2 answers

✓ Accepted answer

I believe that the error message is referring to what you have posted as line 7. You attempted to FireClient, but forgot to provide which player you are "firing" to. That's why the error message is strange -- it attempted to convert whatever slot is into a Player (which it called an Object here) and failed.

Unless your server only supports 1 player, this server script is probably not flexible enough. Your server script needs to be able to handle the inventory for all players, not just one.

Minor note: Saying {nil, nil, ...} (your 1st line) is the same as saying {}.

1 comment

0

Thanks for letting me know, I got it to work now.

This discussion is preserved as it was. New answers, comments, and votes are closed.