ServerScript or LocalScript In Gui? - Cloning a tool to the player's Backpack

I started out by scripting it ServerSide but then I remembered how everyone always preaches about how you should only use LocalScripts in Guis. Is it better to use a LocalScript or a ServerScript in this case? Also, is it better to clone the gun from ServerStorage or ReplicatedStorage? Why?

Method 1 - ServerSide
ServerScript in TextButton:

local Player = script.Parent.Parent.Parent.Parent.Parent
local Gun = game.ServerStorage.Tools.Gun

function onclick()
	if Player.Stats.Gun.Value == true then
		Gun:Clone()
		Gun:Clone().Parent = Player.Backpack
	end
end

script.Parent.MouseButton1Click:connect(onclick)

Or
Method 2 - ClientSide
LocalScript in TextButton:

local Player = game.Players.LocalPlayer
local Event = game.ReplicatedStorage.Tools.GiveGun

function onclick()
	if Player.Stats.Gun.Value == true then
		Event:FireServer()
	end
end

script.Parent.MouseButton1Click:connect(onclick)

ServerScript in ServerScriptService:

local Event = game.ReplicatedStorage.Tools.GiveGun
local Gun = game.ServerStorage.Tools.Gun

Event.OnServerEvent:Connect(function(Player)
	if Player.Stats.Gun.Value == true then
		Gun:Clone()
		Gun:Clone().Parent = Player.Backpack
	end
end)

Using a local script to detect MouseButton1Click and firing a remote event to let the server do the rest of the work is highly suggested and recommended.

I dont think server scripts can/should be used for calling the MouseButton1Click events. And it doesnt really matter about where the tool is placed in, as long as you are not cloning the tool from server storage in a LocalScript (which I highly discourage), because LocalScripts cannot access ServerStorage.

Second method is best, so use the second method.

2 Likes

I recommend using local script since you also check for the player’s data on the server.

The first method works with no errors, I scripted and tested both. I’m trying to understand why one method would be favorable over the other. Why go through the the trouble of having to use a LocalScript, normal Script, and a RemoteEvent when the same thing can be accomplished in a single server script?

1 Like

I believe using remote events for server and client communication is a well organized way of achieving what you want to do (when using MouseButton1Click event). Although this isnt the actual reason why you should use the second method instead of the first.
The MouseButton1Click event is meant to be used in LocalScripts, not Server Scripts. There are other reasons as well, but I am not aware of them.

But I really recommend you use LocalScripts to use the MouseButton1Click event, and use a remote event to let the server (script) do the rest of the work.

I appreciate you taking the time to write a response but I don’t see how having two separate scripts and a RemoteEvent is more organized than having a single server script.

I’m not doubting that method 2 is the proper way to do it. The main reason I made this post was to understand why it’s better to do it on the client but I haven’t seen anything convincing thus far.

It’s never specified in the api reference that MouseButton1Click event is meant to be used in LocalScripts only. GuiButton | Roblox Creator Documentation

1 Like

It’s not always about what is more organized. When firing The MouseButton1Click event, you should use a LocalScript. The server script is able to fire the event, but there are limitations to it and if you wanted to do more with the button, it would become more difficult and you would run into issues that wont be able to be fixed while using a server script.

In this case, you can use either method because both are able to run the event.