The goal:
I’m attempting to make a GUI menu that, on click, clones a tool located in ReplicatedStorage and moves it to a player’s backpack.
The problem:
Tools will clone and move just fine, except any script that is in that tool is completely broken. (i.e. Bloxy Cola script, qPerfectionWeld)
When a qPerfectionWeld script is broken, it acts like I’m just cloning a straight up anchored tool to the backpack, which means the tool stays at its original location in the world, and equipping it ‘teleports’ the player to the tool.
The LocalScript that does the cloning after clicking a button:
local tool = game.ReplicatedStorage["Tool Name"]
local player = game.Players.LocalPlayer
script.Parent.MouseButton1Click:Connect(function()
tool:Clone().Parent = player.Backpack
end)
I’ve found a topic that is my exact same issue, except the responses were, well, completely unhelpful in actually explaining how to solve the issue.
I know I should use RemoteEvents, but how am I supposed to properly utilise them in this particular case? I’ve found no code examples so far that demonstrate properly cloning a tool to a backpack.
You’re cloning the tool on the client, which breaks some client-server replication behavior since the server can’t access the cloned tool.
Pretty easy to rectify though: It’s pretty much the same code but run server-side via a RemoteEvent.
—Client
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ToolRemote = (…) —set to directory of the remote you want to use
local ToolToClone = (ReplicatedStorage…) —tool you wish to clone
…MouseButton1Click:Connect(function()
ToolRemote:FireServer(ToolToClone)
end)
—Server
local ToolRemote = (…) —set to directory of the remote you want to use
ToolRemote.OnServerEvent:Connect(function(Player, Tool)
local NewTool = Tool:Clone()
NewTool.Parent = Player.Backpack
end)
might be some typos b/c i wrote this on mobile but yeah
I will attempt to set this up and will edit this reply with any updates
But I’d like to know where I should put the scripts, I’m assuming the client script is the one I’m already using in the button, but I have no clue where the server script must go (nor the RemoteEvent)
Server scripts can run practically anywhere that the server can access, that includes workspace, serverscriptservice, characters, and basically anything that isn’t instanced solely by the client.
As for the remote event, just make sure that both the server and client can see it, usually id dump it into a folder called “Remotes” inside of replicated storage but workspace and tools themselves can work just fine
Alright that worked! Will mark your code examples as solutions for other people with the same problem, and I’ll put my set up below for reference.
It even plays nicely when there are multiple tools!
Locations of involved items (selected)
LocalScript:
local RepStorage = game.ReplicatedStorage
local ToolGUIRemote = RepStorage.ToolGUIEvent
local TargetTool = RepStorage["Husky Cola"] --Replace the string with the desired tool
script.Parent.MouseButton1Click:Connect(function()
ToolGUIRemote:FireServer(TargetTool)
end)
ToolGUIHandler script:
local RepStorage = game.ReplicatedStorage
local ToolGUIRemote = RepStorage.ToolGUIEvent
ToolGUIRemote.OnServerEvent:Connect(function(Player, Tool)
local ClonedTool = Tool:Clone()
ClonedTool.Parent = Player.Backpack
end)
The scripts in the tools located in ReplicatedStorage are enabled by default
*Oops, noticed I had a redundant variable in the local script for the local player, removed that line…