Why is this code not working?

This code is supposed to get the name of an item (on the client), then pass it to the server so it can update the value of a string (server side). The client and the server give different outputs for the same variable.
Client

script.Parent.Hitbox.MouseButton1Click:Connect(function(player)
	if script.Parent.ItemName then
		local Plant = string.gsub(script.Parent.ItemName.Value, "Seeds", "")
		print(Plant)--prints Carrot (Carrot is the plant used for testing)
		game.ReplicatedStorage.Farming.UpdateSelection:FireServer(player,Plant)
		end
end)

Server

game.ReplicatedStorage.Farming.UpdateSelection.OnServerEvent:Connect(function(player,Plant)



print(Plant)--Prints nil

game.ServerStorage.Values:WaitForChild("SelectedSeed").Value = Plant

end)

You should be able to just fire the ‘Plant’ to the server:

game.ReplicatedStorage.Farming.UpdateSelection:FireServer(Plant)
2 Likes

Should the parameters stay the same on the server?

Yes. The first parameter in the connection function of OnServerEvent is always the player instance who fired the remote event. In other words, the first argument you put in the function on the client will always be the second parameter of the connected function on the server.

1 Like

Yes as @ItzMeZeus_IGotHacked has already said. You should only need to remove it from the client like I have displayed in my first post.

1 Like

I didn’t know this. Thanks for the information and help, both of you!

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