How can i make a roblox client spawned object visible to other players too?(Server)

  1. I made a Spawn gui panel where you can spawn objects

  2. The script works but it is only visible to the client and not to other players

i used a localscript inside the gui buttons
Code:
local repstorage = game:GetService(“ReplicatedStorage”)
local players = game:GetService(“Players”)
local player = players.LocalPlayer
local character = player.Character
local humroot = character:WaitForChild(“HumanoidRootPart”)
local model = repstorage:WaitForChild(“Barrel”)

script.Parent.MouseButton1Click:Connect(function()
script.Parent.Sound:Play()
local modelclone = model:Clone()
modelclone.Parent = workspace.Props
modelclone.CFrame = humroot.CFrame * CFrame.new(0, 0, -5)
end)

1 Like

The easiest way is to just spawn it on the server. Make a remote event through which the client can tell the server what the player wants to spawn. On the server side, check for the item and then spawn it into the workspace which should replicate to all the clients.

1 Like

can you show me an example please? I never used remote events.

You could make a localscript inside buttons and making a script for example like this

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.Character
local humRoot = character:WaitForChild("HumanoidRootPart")
local model = ReplicatedStorage:WaitForChild("Barrel")
local spawnObjectEvent = ReplicatedStorage:WaitForChild("SpawnObjectEvent")

script.Parent.MouseButton1Click:Connect(function()
    script.Parent.Sound:Play()
    spawnObjectEvent:FireServer(humRoot.CFrame * CFrame.new(0, 0, -5))
end)

and in ur serverscript u could do this

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local spawnObjectEvent = ReplicatedStorage:WaitForChild("SpawnObjectEvent")
local PropsFolder = workspace:WaitForChild("Props") -- Make sure you have a Props folder in workspace

spawnObjectEvent.OnServerEvent:Connect(function(player, cframe)
    local model = ReplicatedStorage:WaitForChild("Barrel")
    local modelClone = model:Clone()
    modelClone.Parent = PropsFolder
    modelClone:SetPrimaryPartCFrame(cframe)
    modelClone:MakeJoints()
    -- Replicate object creation to all clients
    ReplicatedStorage:WaitForChild("ObjectReplicationEvent"):FireAllClients(modelClone)
end)

and in a localscript for all the clients:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local objectReplicationEvent = ReplicatedStorage:WaitForChild("ObjectReplicationEvent")

objectReplicationEvent.OnClientEvent:Connect(function(modelClone)
    modelClone.Parent = workspace.Props -- Assuming Props folder is replicated to all clients
end)

make sure to create remoteevents named “SpawnObjectEvent” and “ObjectReplicationEvent” in ReplicatedStorage

2 Likes

It does work but it doesent clone it to my position. What do i do now? edit: oh wait mb i think i know how to fix it. You scripted it like it was a model but its a single part it is a union. edit 2: yup i got it thank you so much man i really appreciate it!

2 Likes

no problem man, always here to help people

2 Likes

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