Parameter in client event being recieved as nil

I am trying to send a parameter to the client from a server script, but when the client receives the remote event with the parameter, it is accepted as nil.

There are no errors in the output.

Server Code:

for _,v in ipairs(game.Players:GetPlayers()) do
local Tool = game.ServerStorage.Folder:GetChildren()[math.random(1,#game.ServerStorage.Folder:GetChildren())]
		v.Data.Data1.Value = Tool.Name
		print(Tool)
-- Part of the code that does not work
		plr = game.Players:FindFirstChild(v.Name)
		game.ReplicatedStorage.Remote:FireClient(plr, Tool)
end

Client Code:

game.ReplicatedStorage.Remote.OnClientEvent:Connect(function(Tool)
-- Tool prints as nil
	print(Tool)
	local plr = game.Players.LocalPlayer
	script.Parent.Frame.ImageLabel.Image = game.ReplicatedStorage.OtherFolder:FindFirstChild(Tool.Name).TextureId
	end)

How do I fix this?

If needed, I can send more parts of the script.

Move the tool to ReplicatedStorage or somewhere else; the client can’t access ServerStorage. You could also clone it and move it elsewhere first.

If you look at the code snippets again, the client code is accessing ReplicatedStorage, and the server code is accessing ServerStorage.

You’re using Tool.Name, which is nil because Tool was passed from the server to the client. Once you move the Tool to ReplicatedStorage or clone it THEN move it, your script should work once you make the changes to it.

When firing the remote to the client, would it not be useful to use the Player v value instead of creating a new reference? Making the plr value in the server script is redundant because you’re already looping through the table of players.

using the v value didn’t work either, so I tried using plr instead.

They also have a good point.

Instead of doing:

for _,v in ipairs(game.Players:GetPlayers()) do
local Tool = game.ServerStorage.Folder:GetChildren()[math.random(1,#game.ServerStorage.Folder:GetChildren())]
		v.Data.Data1.Value = Tool.Name
		print(Tool)
-- Part of the code that does not work
		plr = game.Players:FindFirstChild(v.Name)
		game.ReplicatedStorage.Remote:FireClient(plr, Tool)
end

You can do this:

for _,v in pairs(game.Players:GetPlayers()) do
	local Tool = game.ServerStorage.Folder:GetChildren()[math.random(1,#game.ServerStorage.Folder:GetChildren())]
	v.Data.Data1.Value = Tool.Name
	
	game.ReplicatedStorage.Remote:FireClient(v, Tool)
end

Your problem is with the tool.

I changed it so the tools are cloned to replicatedstorage then the remote is fired, but the tool still prints as nil in the client script.

Update: Fixed it by moving the folder in ServerStorage to ReplicatedStorage.

Whenever you send an instance through a remote event, the client will only receive it if that instance exists on the client.

In your case, you are providing a tool that is in ServerStorage, all children/descendants of ServerStorage are not accessible to the client.

In order to fix this, you will want to move the tool to ReplicatedStorage before its sent to the client. (Or anywhere the client has access too.) You could simply have the tool in ReplicatedStorage in the first place, then just fetch a random tool from there, without having to change the parent via script.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.