Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why dosen't this script work?

This script is to be able to equip and unequip a sword by clicking the TextButton. Here's the script:

local button = script.Parent
local plr = game.Players.LocalPlayer
local char = plr.Character
local Backpack = char.BackPack
local excal = script.Parent.Parent.Parent.Parent.Parent.Parent.Parent.ServerStorage.Excalibur


script.Parent.MouseButton2Click:connect(function()
	excal:Clone().Parent = plr.BackPack
end)

script.Parent.MouseButton1Click:connect(function()
	if  excal.Parent == plr.BackPack then
		excal:Destroy()
		
	end
end)

2 answers

✓ Accepted answer

On line 03, the character probably won't even be loaded yet. But you never use it, nor do you use the variable on line 04, so we'll just get rid of both of those. You also never use your button variable.

Your excal variable is pretty ridiculous. That's a freakin' lot of parents. ServerStorage is a child of game, so you can just write

game.ServerStorage

instead of all those parents.

But you can't even use ServerStorage in LocalScripts, because it doesn't get replicated to the client. So use ReplicatedStorage instead.

You also have several errors in your spelling. Backpack doesn't have a capital P.

Another error is encountered where you check excal's Parent. excal's Parent is ServerStorage, and it will never be anything else. Its clone might be in the backpack, but it won't be. To fix this, use FindFirstChild to look for the object in the backpack by name.

local button = script.Parent
local plr = game.Players.LocalPlayer
local excal = game.ReplicatedStorage.Excalibur

button.MouseButton2Click:connect(function()
	excal:Clone().Parent = plr.Backpack
end)

button.MouseButton1Click:connect(function()
	if plr.Backpack:FindFirstChild("Excalibur") then
		plr.Backpack.Excalibur:Destroy()
	end
end)

referenced backpack incorrectly, backpack is in the local player not the character, try this instead:

local Backpack = plr.BackPack

This discussion is preserved as it was. New answers, comments, and votes are closed.