How can I send data like a part from client to server?

I have a client → server remote function. But I don’t know how you can send data to the server like a part for example so I can reference that part in the server script?

1 Like

You can’t send objects through remote events. You can send the object:GetFullName() or object.Name over in the remote event which will help you reference the part in the server script.

Edit: I was misinformed a while ago and this does not apply. The only reason sending an object didn’t work was because the object was only client sided, so sending it to the server would not work.

3 Likes

like this?

local script:

local Event = game.ReplicatedStorage:WaitForChild("Event")

local newPart = Instance.new("Part")
newPart.Parent = workspace

Event:FireServer(newPart)

script:

local Event = game.ReplicatedStorage:WaitForChild("Event")

Event.OnServerEvent:Connect(function(player, newPart)
       -- change part's properties or idk
end)

Hello, Matt! Isn’t it more logical to develop the part on the server and then communicate data from there to the client? Of course, you never know what the client might communicate to the server (security concerns). Could you also give a specific example of what you’re trying to attempt?

1 Like

You can’t send Instances through remote events so this wouldn’t work.

I’ve got a character creation system, when you click a button that is a “Hair style” in this case. I store the hair model inside the button, and put the hair on in the server script. Atm the hair is just in the workspace. But I want to transfer the Hair to the button to keep everything neat, also I don’t know which button is pressed. Unless you can pass button data to the server?

So not a part, a part that has already been created

You’re better off storing your assets somewhere like ReplicatedStorage, and firing a remote with data (such as a string) that corresponds to that particular asset, e.g. assigning an ID to each hair asset and applying it on the server when the user selects it.

2 Likes

Pass the reference of the hair to the server, and then equip it on the server.

Oh That makes alot more Sense ! Try something like this (Beginner Example) :

local script:

local Event = game.ReplicatedStorage:WaitForChild("Event")
local Button = ("Refrence ur button")
local CurrentHair = nil

Button.MouseButton1Click:Connect(function()
 CurrentHair = "01"
 Event:FireServer(CurrentHair)
end)

script:

local Event = game.ReplicatedStorage:WaitForChild("Event")
local Character = nil
Event.OnServerEvent:Connect(function(player, Hair)
      Character = player.Character or Player.CharacterAdded:Wait()
           if Hair == "01" then
            --- get the hair from somewhere and put it on the character
           end
end)
3 Likes

Dunno if that is secure in anyway, however it does print. And I am able to click on the output with the given part
image
BUT This won’t work for created instances on the client (i believe?)

localscript:

game.ReplicatedStorage.RemoteEvent:FireServer(workspace.Baseplate)

server-script:

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(plr, part)
    print(part, part.Name)
end)

Still doesn’t work. Like I said before, you can not send any instance through remote events.

you maybe didn’t understand me

local Event = game.ReplicatedStorage:WaitForChild("Event")

local Part = workspace.Part

Event:FireServer(Part)

Oh, sorry, yea I didn’t understand.

Would this be safe? What if client alters part and sends the altered part to server?

Are you able to tell me what is wrong here? I’m trying to attach a hat to my characters head in the workspace. This worked when I was using the remotefunction, but now changing to this method it’s no longer working. It creates the hat but it creates the hat on top at the origin and it just falls instead of attaching to the character. Any ideas?

local script:


local ReplicatedStorage = game:GetService("ReplicatedStorage")


local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent1")
local Button = script.Parent
local styleType = {"Hair", "1"}

Button.MouseButton1Click:Connect(function()
	Event:FireServer(styleType)
end)

server script:

local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent1")
local Character = nil
local style = nil
local humanoid = nil
local char = workspace:WaitForChild("CharSelectDummy")
Event.OnServerEvent:Connect(function(player, styleType)
	Character = player.Character or player.CharacterAdded:Wait()
	humanoid = char:WaitForChild("Humanoid")
	if styleType[1] == "Hair" then
		-- Hair types
		if (styleType[2] == "1") then
			style = player.PlayerGui.CharCreationScreen.HairScrollingFrame.ViewportFrame1.Hair:Clone()
			humanoid:AddAccessory(style)
		end
	end
end)

if the hat is an Accessory then just Parent it to the Character you want instead of using the AddAcesorry function. Also maybe create a folder in replicated will all your accessories and reference it from there instead of going all the way in the playergui.

You can, as long as that instance exists on both client and server. A part created on the client cant be sent to the server, and a part inside ServerStorage cant be sent to the client. An instance in Workspace being sent through a remote from the server would be received on the client.

But why isn’t this working?

Doesn’t AddAccessory just parent? Probably wont work I think

1 Like

Yea didn’t work

-- Local script

local ReplicatedStorage = game:GetService("ReplicatedStorage")


local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent1")
local Button = script.Parent
local styleType = {"Hair", "1"}

Button.MouseButton1Click:Connect(function()
	Event:FireServer(styleType)
end)
--Server script
local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent1")
local Character = nil
local style = nil
local humanoid = nil
local char = workspace:WaitForChild("CharSelectDummy")
Event.OnServerEvent:Connect(function(player, styleType)
	Character = player.Character or player.CharacterAdded:Wait()
	humanoid = char:WaitForChild("Humanoid")
	if styleType[1] == "Hair" then
		-- Hair types
		if (styleType[2] == "1") then
			style = player.PlayerGui.CharCreationScreen.HairScrollingFrame.ViewportFrame1.Hair:Clone()
			style.Parent = char
		end
	end
end)