Help with cloning an item from serverstorage

Hi! I am having trouble with my script

  1. Clone a item from serverstorage to the players backpack

  2. When the script runs the weapon is not cloned and this error is displayed
    " Players.Airirusu.PlayerGui.USARGUI.Main.Deploy.Script:5: attempt to index nil with ‘Clone’ "

I don’t know what I can do to fix it any help would be appreciated!

local plr = script.Parent.Parent.Parent.Parent.Parent
local button = script.Parent.Parent.Primary
if button.MouseButton1Down then
	local weapon = game.ServerStorage:FindFirstChild(button.Text,true)
	local clone = weapon:Clone()
	clone.Parent = game.StarterGear
end

The client script can’t access the server. You need to use a remote event in order to clone the object from the server.

1 Like

This is a server script located in a gui

Your checking if the function MouseButton1Click exists, not when it clicks. Because of this, it fires immediately. The text is likely blank so it wont be able to get the correct weapon causing the :Clone error.

I see then you would have to check if the weapon exist in the server storage:

local weapon = game.ServerStorage:FindFirstChild(button.Text,true)

if weapon then
    ...
end

Just notice button.MouseButton1Down is a event signal so it will always become true for the if statement.

button.MouseButton1Down:Connect(function()
    ...
end)

the text of the button changes depending on if a button was clicked in the same gui
so if the player selects WeaponA the text of the button will change to WeaponA

Personally, I’d use a LocalScript and a RemoteEvent, however, if you wanted to use a ServerScript, I’ve left a snippet of code below that should work if you edit it to suit your workspace.

local ServerStorage = game:GetService("ServerStorage") --Server storage
local Tool = ServerStorage:FindFirstChild("Tool") --Finding the tool
local Button = script.Parent.TextButton --Button

Button.MouseButton1Click:Connect(function() --Fires the code below when someone clicks the button variable (TextButton).
	local Plr = script.Parent.Parent.Parent --Finding the player
	local Clone = Tool:Clone() --Cloning the Tool variable (Cloning the tool in ServerStorage)
	Clone.Parent = Plr.Backpack --Setting the parent of the cloned tool as the backpack of the player ()
end)

This piece of code works but the problem is that tool given must change based on a value or in this case the text of the button

I see, you can probably just check what the Text of the button is and then give the appropriate tool.

local ServerStorage = game:GetService("ServerStorage") --Server storage
local Tool = ServerStorage:FindFirstChild("Tool") --Finding the tool
local Tool2 = ServerStorage:FindFirstChild("Tool2") --Finding another tool, in this case, the second tool.
local Button = script.Parent.TextButton --Button

Button.MouseButton1Click:Connect(function() --Fires the code below when someone clicks the button variable (TextButton).
	
	if Button.Text == "Option1" then
		local Plr = script.Parent.Parent.Parent --Finding the player
		local Clone = Tool:Clone() --Cloning the Tool variable (Cloning the tool in ServerStorage)
		Clone.Parent = Plr.Backpack --Setting the parent of the cloned tool as the backpack of the player ()
	elseif Button.Text == "Option2" then
		local Plr = script.Parent.Parent.Parent --Finding the player
		local Clone = Tool2:Clone() --Cloning the Tool variable (Cloning the tool in ServerStorage)
		Clone.Parent = Plr.Backpack --Setting the parent of the cloned tool as the backpack of the player ()
	end
	
end)

This would work but with a larger selection of tools the script would be hundreds of lines long
Couldn’t the script just try and find a tool with the same name as the text of the button?

local Tool = ServerStorage:FindFirstChild(Button.Text,true)

Something like this maybe? though i have tried it with no success

Yeah I see what you mean. I’ve edited my original code to work a bit better.

I’ve gave a brief explanation on what each line of code is doing, hope this helps!

local ServerStorage = game:GetService("ServerStorage") --Server storage
local Tool = ServerStorage:FindFirstChild("Tool") --Finding the tool
local Tool2 = ServerStorage:FindFirstChild("Tool2") --Finding another tool, in this case, the second tool.
local Button = script.Parent.TextButton --Button

local Tools = {} --Dictionary

function AddToTable() --Function for adding the tools to the table above.
	for _, v in pairs (ServerStorage:GetChildren()) do
		if v:IsA("Tool") then
			table.insert(Tools, v.Name)
		end
	end
end

Button.MouseButton1Click:Connect(function() --Fires the code below when someone clicks the button variable (TextButton).
	
	AddToTable() --Firing the function
	wait(0.1) --debug
	
	if table.find(Tools, Button.Text) then --Checking if the text of the button is in the table.
		local Plr = script.Parent.Parent.Parent --Finding the player
		local Clone = ServerStorage:FindFirstChild(Button.Text):Clone() --Finding the tool and cloning it
		Clone.Parent = Plr.Backpack --Setting the parent of the cloned tool as the backpack of the player (giving the player the tool)
	else
		return
	end
	
	
end)

Please resort to using replicated storage. It can be accessed by the client and server therefore it is much better. You should also use a local script and fire a remote event from the local script when the Remote Event is fired you can give the tool directly to the player which would be the first argument of the OnServerEvent(). Please let me know if you need assistance with doing so.

@Airirusu Consider putting all of the tools inside a folder in replicated storage then looping through them. So it’s only a few lines long.

I will create a script for you if you want.

For some odd reason the tools being in replicatedstorage breaks the tools the only place the tools work is when there stored in serverstorage

That is most likely due to the fact that you are not using a remote event.

local plr = game.Players.LocalPlayer
local button = script.Parent.Parent.Primary
button.MouseButton1Click:Connect(function()
	local weapon = game.ServerStorage:FindFirstChild(button.Text,true)
	local clone = weapon:Clone()
	clone.Parent = plr.Backpack
end)

Try this and it should work.

@Airirusu Go ahead and try this. I made a game my script. Try seeing if this works for you.
Tool_Game.rbxl (45.4 KB)

Still responds with attempt to index nil with Clone