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

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

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

✓ Accepted answer

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)

3 comments

0

OHHHHHHHHHHHHHHHHHHHHHHHH thank you so much!!!!!!!!!!!!!!!!!!!!!!!!!!

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!

0

Haha no problem!

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