Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Workspace and Server Replication?

Asked by 5 years ago

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?

0
Can you provide code? User#19524 175 — 5y

1 answer

Log in to vote
2
Answered by
amanda 1059 Moderation Voter
5 years ago

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)
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. User#19524 175 — 5y
Ad

Answer this question