Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Workspace and Server Replication?

I am having a problem with server replication. I want to click a button and spawn an object but when I use a LocalScript in StarterGui to clone a Part and its children and then parent the clone to Workspace, the item I spawned only shows up in the client-side and nothing appears in the server. What is the best way to put it in both client and server? Also, is there a way to control how it is added to the client and server? For example, is it possible to have it replicated like what happens to Workspace in the beginning of the game where all items in it are replicated to the server and client?

Another question to piggyback off of that one I was calling a RemoteFunction from a Script in Workspace. The RemoteFunction was linked to a LocalScript in the StarterGui whose sole line of code was to return the player mouse. When it returned the mouse, it only showed up in the client side and not the server side. Why is that and how do I fix this?

1 comment

0

Can you provide code?

1 answer

✓ Accepted answer

Hey there!

Lets look at both of your problems.

GuiButton that creates part visible to Server and Client

This one is easier than you might think. All you have to do, is create the part server-side, and it will automatically replicate to the clients.

To accomplish this, set up a RemoteEvent that fires to the server once you click the GuiButton.

Client

local repstore = game:GetService("ReplicatedStorage")
local CreatePart = repstore:WaitForChild("CreatePart") --RemoteEvent
local gui = script.Parent

gui.MouseButton1Click:Connect(function()
	CreatePart:FireServer()
end)

Server

local repstore = game:GetService("ReplicatedStorage")
local CreatePart = repstore:WaitForChild("CreatePart") --RemoteEvent

CreatePart.OnServerEvent:Connect(function(player)
	local char = player.Character
	if char then
		local p = Instance.new("Part")
		p.Size = Vector3.new(1, 1, 1)
		p.Color = Color3.new(1, 0, 0)
		p.Shape = Enum.PartType.Ball
		p.Anchored = true
		p.CanCollide = false
		p.CFrame = char.HumanoidRootPart.CFrame * CFrame.new(0, 0, -5)
		p.Parent = workspace
	end
end)

Sending the Mouse through a Remote to the Server

This is not possible. You cannot send the actual mouse object through a remote, because it is strictly client-side.

However, you can send the position where the mouse clicked, and/or the BasePart the mouse clicked on.

Client

local repstore = game:GetService("ReplicatedStorage")
local SendMouseInfo = repstore:WaitForChild("SendMouseInfo") --RemoteEvent

local players = game:GetService("Players")
local player = players.LocalPlayer
local mouse = player:GetMouse()

mouse.Button1Down:Connect(function()
	local target = mouse.Target
	local hit = mouse.Hit
	if target then
		SendMouseInfo:FireServer(target, hit.p)
	end
end)

Server

local repstore = game:GetService("ReplicatedStorage")
local SendMouseInfo = repstore:WaitForChild("SendMouseInfo") --RemoteEvent

SendMouseInfo.OnServerEvent:Connect(function(player, target, position)
	print("player: "..player.Name)
	print("target: "..tostring(target))
	print("position: "..tostring(position))
end)

1 comment

0

yeah, clears things up. many users i’ve seen in the past try to pass the mouse as a parameter, the object. not the target or hit. some even try using mouse events in the server.

This discussion is preserved as it was. New answers, comments, and votes are closed.