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

Add/Remove Tool GUI script for button doesn't want to work twice?

Asked by 7 years ago
Edited 7 years ago

The script is used for a GUI. When a player clicks on the GUI button, if they do not have the tool in their backpack it will give them the tool. If they already have the tool, then it will remove the tool. Right now, the script will give me the tool, but if I click again it won't removing it.

However, if I put the tool in the StarterPack and run the script, it removes the tool. It just doesn't give it back again upon a second click. So the remove tool function works, however it doesn't seem to want to work together, or want to work after each other.

local plr = game.Players.LocalPlayer
local tools = plr.Backpack

Player=script.Parent.Parent.Parent.Parent

function ToolRemove()


     if tools:findFirstChild("Tool") then


        tools.Tool:Remove()
        script.Parent.Selected=true
        wait(.1)
    end 
end

function ToolAdd()

    if game.Lighting:findFirstChild("Tool") then


        Tool=game.Lighting.Tool:clone()
        Tool.Parent=Player.Backpack
        script.Parent.Selected=true
        wait(.1)
    end
end

script.Parent.MouseButton1Down:connect(function()
    if script.Parent.Selected==false then
            ToolRemove()
            ToolAdd()
    end
end)

Thank you very much!

1 answer

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

Here is a corrected script, you were really close! Your problem was mainly the MouseButton1Down function.

Remove() is deprecated, use Destroy()

local plr = game.Players.LocalPlayer
local tools = plr.Backpack
local Tool = game.Lighting.Tool
Player=script.Parent.Parent.Parent.Parent


function ToolRemove()


     if tools:FindFirstChild("Tool") then


        tools.Tool:Destroy()
        wait(.1)
    end 
end

function ToolAdd()

    if game.Lighting:FindFirstChild("Tool") then


        Tool=game.Lighting.Tool:clone()
        Tool.Parent=Player.Backpack
        wait(.1)
    end
end

script.Parent.MouseButton1Down:connect(function()
        if Player.Backpack:FindFirstChild("Tool") then
        ToolRemove()
else
        ToolAdd()

    end
end)
0
OHHHHHHHHHHHHHHHHHHHHHHHH thank you so much!!!!!!!!!!!!!!!!!!!!!!!!!! callmehbob 54 — 7y
1
I was helping her write the script, I was trying to make the else but I failed so i got rid of it and that was my code we ended up with, I can't believe we were that close, thanks for the fast reply too! DJ_Clone 1 — 7y
0
Haha no problem! GuestRealization 102 — 7y
Ad

Answer this question