Cloning Tool through Local Script/Server Script

Basically, once a String Value is changed, the player is given a tool corresponding to the NewValue

However, the tool completely breaks.

I am wondering if this is because I am cloning the tool through a LocalScript instead of a ServerScript?

local player = game.Players.LocalPlayer
local backpack = player.Backpack
local rs = game:GetService("ReplicatedStorage")

script.Parent.Changed:Connect(function(newvalue)
	if newvalue == "Jedi" then
		DeleteOther()
		script.Parent.Parent.EquippedText.Text = "Selected: "..newvalue
		local clone = rs.Jedi:Clone()
		clone.Parent = backpack
	elseif newvalue == "Sith" then
		DeleteOther()
		script.Parent.Parent.EquippedText.Text = "Selected: "..newvalue
		local clone = rs.Sith:Clone()
		clone.Parent = backpack
	elseif newvalue == "Obi-Wan" then
		DeleteOther()
		script.Parent.Parent.EquippedText.Text = "Selected: "..newvalue
		local clone = rs["Obi-Wan"]:Clone()
		clone.Parent = backpack
	end
end)  

function DeleteOther() -- Deletes other lightsabers in the inventory
	for _, p in pairs(backpack:GetDescendants()) do
		if p:IsA("Tool") then
			if p.Name == "Sith" or "Jedi" or "Obi-Wan" then
				p:Destroy()
			end
		end
	end
end

no errors btw

1 Like

It seems like there might be a couple of issues in your code that we could potentially address:

  1. Conditional in DeleteOther function: The condition if p.Name == "Sith" or "Jedi" or "Obi-Wan" does not do what you expect. Instead, it evaluates as if p.Name == "Sith" or true or true, resulting in always being true. You should explicitly compare each value.
  2. Potential issue with cloning tools: Cloning tools from replicated storage directly on the client-side could lead to some issues, especially if there’s a delay in asset replication or if the client doesn’t have access to those assets. It’s generally safer to clone tools on the server and then send them to the client.

Here’s how you can modify your code to address these issues:

local player = game.Players.LocalPlayer
local backpack = player.Backpack
local rs = game:GetService("ReplicatedStorage")

script.Parent.Changed:Connect(function(newvalue)
    local toolName = newvalue
    
    if toolName == "Jedi" or toolName == "Sith" or toolName == "Obi-Wan" then
        DeleteOther()
        script.Parent.Parent.EquippedText.Text = "Selected: "..toolName
        
        -- Request server to give the player the tool
        game.ReplicatedStorage.RemoteEvent:FireServer(toolName)
    end
end)  

function DeleteOther() -- Deletes other lightsabers in the inventory
    for _, p in pairs(backpack:GetChildren()) do
        if p:IsA("Tool") then
            if p.Name == "Sith" or p.Name == "Jedi" or p.Name == "Obi-Wan" then
                p:Destroy()
            end
        end
    end
end

On the server-side (in a Script), you’ll need to handle the tool giving logic:

local rs = game:GetService("ReplicatedStorage")
local remoteEvent = rs.RemoteEvent -- Replace with the name of your remote event

remoteEvent.OnServerEvent:Connect(function(player, toolName)
    local backpack = player.Backpack
    local clone = rs[toolName]:Clone()
    clone.Parent = backpack
end)

This way, the tool cloning happens on the server, which is generally safer. The client sends a request to the server to give the player the requested tool, and the server responds by giving the tool to the player. You will need to add in a remote event, and replace local remoteEvent with the name of your Remote Event.

Cloning a tool through local scripts would mean that other players won’t be able to see it… Consider cloning the tool through the server