How would I pass a table from the Server to the Client instead of Client to Server

Hi, I want to pass a table from a server script to a local script by using a FireClient from the server and catch it in the client with a OnClientEvent.

I tried it but I got some errors:

13:14:58.565 - Players.Grayuu.PlayerGui.SideBar.LocalScript:2: invalid argument #1 to 'ipairs' (table expected, got nil)
13:14:58.568 - Stack Begin
13:14:58.570 - Script 'Players.Grayuu.PlayerGui.SideBar.LocalScript', Line 2
13:14:58.571 - Stack End

Here is the OnClientEvent script:

game.ReplicatedStorage.SideBar.GiveColoursToClient.OnClientEvent:Connect(function(player,ColorTable)
	for index, value in ipairs(ColorTable) do
		print(index, value)
	end
end)

And this is how I pass the table:

local ColorTable = {example,example,example}

game.ReplicatedStorage.SideBar.GiveColoursToClient:FireClient(player,ColorTable)

All the help is appreciated, if you need more information just tell me :happy3:

.OnClientEvent event only returns the extra arguments you passed in :FireClient().

1 Like

Greetings,

Whenever a client receives an event, it doesn’t have the automatic “player” argument, you should leave it as this:

game.ReplicatedStorage.SideBar.GiveColoursToClient.OnClientEvent:Connect(function(ColorTable)
2 Likes

I will try that right away :slight_smile:

I have a slight error. Because I am making a level generating kind of game. I want to send the table when each round starts. I played with my script a bit but it keeps ignoring the first round and not firing the FireClient, but then sends the table on other rounds.

Here is my level generating script:

local level = game.ReplicatedStorage:WaitForChild("GeneratedLevels"):GetChildren()
local studs = 0
local endLevel = game.ReplicatedStorage.End

local ColorTable = {}


while true do
	for count = 1, 6 do
		for i, placeHolder in pairs(game.Workspace.PlaceHolders:GetChildren()) do
			local chosenLevel = level[math.random(1, #level)]:Clone()
			print(chosenLevel.Name)
            -- Inserting values to table
			table.insert(ColorTable, chosenLevel.ColorBar.Value)
			chosenLevel.PrimaryPart = chosenLevel.Floor
			chosenLevel:SetPrimaryPartCFrame(CFrame.new(placeHolder.Position)+ Vector3.new(studs, 0, 0))
			studs = studs - chosenLevel.studs.Value
			
			chosenLevel.Parent = game.Workspace.currentLevels
		end
	end
	
	local endLevelClone = endLevel:Clone()
	endLevelClone.PrimaryPart = endLevelClone.Floor
	endLevelClone:SetPrimaryPartCFrame(CFrame.new(game.Workspace.PlaceHolders.Floor.Position)+ Vector3.new(studs, 0, 0))
	endLevelClone.Parent = game.Workspace.currentLevels
	-- Fire Client Function
	for i, player in pairs(game:GetService("Players"):GetPlayers()) do
		game.ReplicatedStorage.SideBar.GiveColoursToClient:FireClient(player,ColorTable)
	end

	
	local minutes = 1
	local seconds = 0
	
	local minutesVal = game.ReplicatedStorage:WaitForChild("TimerVal"):WaitForChild("minutesVal")
	minutesVal.Value = minutes
	
	local secondsVal = game.ReplicatedStorage:WaitForChild("TimerVal"):WaitForChild("secondsVal")
	secondsVal.Value = seconds
	
	local timer = game.ReplicatedStorage.Timer

	
	repeat
		if seconds <= 0 and secondsVal.Value <= 0 then
			minutes = minutes - 1
			seconds = 59
			
			minutesVal.Value = minutesVal.Value - 1
			secondsVal.Value = 59
		else
			seconds = seconds - 1
			secondsVal.Value = secondsVal.Value - 1
		end
		
		if seconds <= 9 then
			
		timer.Value = tostring(minutes)..":0"..tostring(seconds) else
			
			timer.Value = tostring(minutes)..":"..tostring(seconds)
		end
		
		wait(game.Workspace.Values.WaitSpeed.Value)
		
	until minutes <= 0 and seconds <= 0
	
	if minutes <= 0 and seconds <= 0 then		
		-- Claimed Value
		for _, player in ipairs(game:GetService("Players"):GetPlayers()) do
			if (player) then
				player.ClaimedFolder.Claimed.Value = false
			end
		end
		-- Rep Values for local Buttons
		game.ReplicatedStorage.ShopEvents.LocalButtons.invisibilityButton.Value = false
		game.ReplicatedStorage.ShopEvents.LocalButtons.BunnyHopButton.Value = false
		game.ReplicatedStorage.ShopEvents.LocalButtons.FogButton.Value = false
		game.ReplicatedStorage.ShopEvents.LocalButtons.HighSpeedButton.Value = false
		game.ReplicatedStorage.ShopEvents.LocalButtons.LowGravityButton.Value = false
		game.ReplicatedStorage.ShopEvents.LocalButtons.invincibilityButton.Value = false
		
		-- Bought Values
		script.Parent.ShopItems.BunnyHopFolder.bought.Value = false
		script.Parent.ShopItems.invisivilityFolder.bought.Value = false
		script.Parent.ShopItems.FogFolder.bought.Value = false
		script.Parent.ShopItems.LowGravityFolder.bought.Value = false
		script.Parent.ShopItems.HighSpeedFolder.bought.Value = false
		script.Parent.ShopItems.invincibilityFolder.bought.Value = false
		-- Enabled Value example(BunnyHopEnabled)
		script.Parent.ShopItems.BunnyHopFolder.BunnyHopEnabled.Value = false
		script.Parent.ShopItems.invisivilityFolder.InvisibilityEnabled.Value = false
		script.Parent.ShopItems.FogFolder.FogEnabled.Value = false
		script.Parent.ShopItems.LowGravityFolder.LowGravityEnabled.Value = false
		script.Parent.ShopItems.HighSpeedFolder.HighSpeedEnabled.Value = false
		script.Parent.ShopItems.invincibilityFolder.invincibilityEnabled.Value = false
		-- Fog Back to normal ;)
		game.Lighting.FogEnd = 100000
		game.Lighting.FogStart = 0
		game.Lighting.OutdoorAmbient = Color3.fromRGB(128, 128, 128)
		-- Gravity Back to normal ;)
		game.Workspace.Gravity = 196.2
	end
	
	for i, player in pairs(game.Players:GetChildren())do
		player:LoadCharacter()
		wait(.1)
		player:LoadCharacter()
	end
	
	game.Workspace.Values.WaitSpeed.Value = 1
	game.Workspace.Values.SpeedMultiplier.Value = 1	
	
	
	game.Workspace.currentLevels:ClearAllChildren()
	studs = 0 
	
end
	

First of all something I suggest is changing the following piece removing the loop,

to game.ReplicatedStorage.SideBar.GiveColoursToClient:FireAllClients(ColorTable)

Does the current code output any error?

1 Like

the thing is how am I gonna pass the player if I dont even refer the player in my script?

oops lol did not read the whole thing I though it was fire client not fire all clients :sweat_smile:

1 Like

The :FireAllClients(Args) sends the arguments to every client, while with :FireClient(Player, Args) you choose which clients to send the arguments.

1 Like

still ignores the first round do you think its the cause of the OnClientEvent local script? or the FireClient. because after the first round finishes. it reads the table but last rounds table is still inserted :confused:

Its like if it dosent want to fire to the client…

Here is what the output shows:

  1 Lapis
  2 Brown
  3 Ghost Grey
  4 Neon Orange
  5 Pink
  6 Pastel Green
  7 Carnation Pink
  8 New Yeller
  9 Pink
  10 Cyan
  11 Lime Green
  12 Maroon

It should normaly only show 6 values in the table, so it still stores the values from the first ever round but dosent fire the event?

You should try to put this right after the while true do so that the round begins with a fresh new table

1 Like

still dosent fire the FireAllClients at the very first ever round but it does generate a new table each round, tho why dosent it fire the event at the first ever round but all the other rounds it fires it fine? should I try to use a player added or something?

In this case using the PlayerAdded would be useless, mind sending the local script part that receives the remote?

sure here is the local script that receives the table I tried debuging with print statements but did not seem to work:

game.ReplicatedStorage.SideBar.GiveColoursToClient.OnClientEvent:Connect(function(ColorTable)
	print("o k")
	for index, value in ipairs(ColorTable) do
		print(index, value)
	end
end)

So the “o k” isn’t printed out right?

yes it dosent print the “o k” at the first round but all the other rounds prints the “o k”

I just noticed something, the script immediately begins, try adding a print right before the :FireAllClients() and put a wait(2) before the while true do

2 Likes

It works, I also tried decreasing the wait time to just wait() still works fine. :slight_smile:

Perfect then it was because it fired the remote even before the players joined.

1 Like

I am gonna try one more thing, and I will put you as a solution thx for sticking with me and helping me out :happy3:

1 Like