Clone a item from serverstorage to the players backpack
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
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.
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)
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.