Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why dosen't this script work?

Asked by 9 years ago

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

Log in to vote
3
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
9 years ago

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)
Ad
Log in to vote
1
Answered by 9 years ago

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

local Backpack = plr.BackPack

Answer this question