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)
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.
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
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.