You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? I have a GUI menu that is cloned to a player. The said player can click a button and the local script clones an item from a folder in Workspace.
What is the issue?
Since it relies on a LocalScript, no one else can see the item except for the client–that’s the issue. But I haven’t found a way to call a LocalPlayer from a server script.
What solutions have you tried so far?
This is the current LocalScript stored in each button:
local menustore= game.Workspace.Items
local item = menustore:WaitForChild("Coffee")
script.Parent.MouseButton1Click:Connect(function()
local cloned = item:Clone()
cloned.Parent = game.Players.LocalPlayer.Backpack
end)
I’ve tried using a server script version:
local menustore= game.Workspace.Items
local item = menustore:WaitForChild("Coffee")
script.Parent.MouseButton1Click:Connect(function(player)
local cloned = item:Clone()
cloned.Parent = player.Backpack
end)
Still no luck.
I found another topic on the developer forums, but it was left unanswered, so I decided to ask here. I appreciate any help on my problem.
So you can’t use server scripts to work on GUIs. Also, keep in mind, anything cloned from a local script is client sided, so no one else can see it unless it is controlled by a server script. For this, you would need a remote event. It goes a little something like this:
Local Script:
local remote = game.ReplicatedStorage.RemoteEvent
script.Parent.MouseButton1Click:Connect(function()
remote:FireServer()
end)
Server Script:
local remote = game.ReplicatedStorage.RemoteEvent
local menustore = game.Workspace.Items
local item = menustore:WaitForChild("Coffee")
remote.OnServerEvent:Connect(function(player)
local cloned = item:Clone()
cloned.Parent = player.Backpack
end)
The server must do this. Anything done on the client, unless given network ownership, will not replicate to others. So, I’d recommend firing a remote event to the server to clone it.
local function onServerEvent(player)
local clone = item:Clone()
clone.Parent = player.Backpack
end
When you clone a tool locally, the server is not able to see that.
And, if there is any server script inside the tool, they won’t work either, as the server doesn’t know the parent has been changed.
You should clone your tools from the server.
Handle UI on the client.
You can also use 1 remote event for multiple items. Let’s say you buy items by clicking a textbutton, and all of the textbuttons are parented by a screengui. What you do is make a folder in the workspace named “Items”, and each item’s name corresponds to the textbutton’s name, like so:
Local Script:
local gui = script.Parent --script is in the screengui
local remote = game.ReplicatedStorage.RemoteEvent
for _, v in pairs(gui:GetChildren()) do
if v:IsA("TextButton") then
v.MouseButton1Click:Connnect(function()
remote:FireServer(v.Name) --fires remote event to server with the information of the item's (or button's) name
end)
end
end
Server Script:
local remote = game.ReplicatedStorage.RemoteEvent
local items = workspace:FindFirstChild("Items")
remote.OnServerEvent:Connect(function(player, item) --player is the client, item is v.Name (or the item's name)
local getitem = items:FindFirstChild(item)
local cloneitem = getitem:Clone()
cloneitem.Parent = player.Backpack
end)