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

It doesn't delete the item??????

Asked by
HjmanYT -1
6 years ago

it wont destroy the item in the backpack

local sword = game.Lighting.Sword
local gun = game.Lighting.Gun
local anotah = game.Lighting.anotahone
local GButton = script.Parent.GButton
local AButton = script.Parent.AButton
local SButton = script.Parent.SButton
local WName = script.Parent.WName
local player = game.Players.LocalPlayer

GButton.MouseButton1Click:connect(function()
    WName.Text = gun.Name
    gun.Parent = player.Backpack
    local clone = sword.Parent
    player:FindFirstChild("Sword"):Destroy()
    player:FindFirstChild("anotahone"):Destroy()
end)

AButton.MouseButton1Click:connect(function()
    WName.Text = anotah.Name
    anotah.Parent = player.Backpack
    local clone = anotah.Parent
    player:FindFirstChild("Sword"):Destroy()
    player:FindFirstChild("Gun"):Destroy()
end)

SButton.MouseButton1Click:connect(function()
    WName.Text = sword.Name
    sword.Parent = player.Backpack
    local clone = gun.Parent
    player:FindFirstChild("Gun"):Destroy()
    player:FindFirstChild("anotahone"):Destroy()
end)
0
If the item is selected it would be in the player's character blowup999 659 — 6y
0
I know but I want to delete it from both so it the player cant get the item again until they choose the class HjmanYT -1 — 6y
0
Lighting is a terrible place to store things. That is why ROBLOX made ReplicatedStorage and ServerStorage. hiimgoodpack 2009 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

I recommend for this that you make a single function, since you have a lot of repeated code. To properly remove items from a player, the code needed is larger than one line (with formatting), so it would become even worse if you had to repeat it.

local function RemoveItems(Player,...)
    -->> ... is a useful thing that can be more than one value (it is a tuple)
    -->> You can call the function with as many values as you like after the player and they will all be in ...
    local ItemsToRemove = {...} -->> Put the added values into a table
    local Backpack = Player:WaitForChild("Backpack")
    local Character = Player.Character

    for _,ItemName in pairs (ItemsToRemove) do -->> Loop through the items to remove
        local Item = Backpack:FindFirstChild(ItemName)
        if Item then -->> The below line will error if it isn't found, so we have this if statement
            Item:Destroy()
        end
    end

    if Character then
        for _,ItemName in pairs (ItemsToRemove) do
            local Item = Character:FindFirstChild(ItemName) -->> Search the player also, because the item may be equipped
            if Item then
                Item:Destroy()
            end
        end
    end
end

-->> Example of it in action
Button.MouseButton1Click:Connect(function()
    RemoveItems(Player,"Sword","Gun","Tree","blahblah") -->> One line to remove 4 items!
end)

Hope I helped!

~TDP

Ad

Answer this question