Tool script issue

hi, I am creating a script of the tool and I have a problem, I want the GUI of the tool to be clickable and can also be activated by an input key, but when I go to test it, the game does not give me errors and the first time that I take the tool, both methods are done, but when I unequip the tool and equip it again, the key doesn’t run, but the click yes. Can anyone help me?

--Here there are the codes of the Animations
local holdAnim = number
local Animation2 = number

--MainCore, here there are all the parts dedicated to the player and the GUI
local player = game.Players.LocalPlayer
local GUI = script.Parent:WaitForChild("AppleGUI", 1)
local LocalPlayer = game.Players.LocalPlayer
local Tool = script.Parent
local humanoid = player.Character:WaitForChild("Humanoid")

--KeyButtons
local UIS = game:GetService("UserInputService")

--Idle Animation
local anim = Instance.new("Animation")
anim.AnimationId = "rbxassetid://" .. holdAnim
local IdleAnimation = LocalPlayer.Character.Humanoid:LoadAnimation(anim)
--Animation1
local anim2 = Instance.new("Animation")
anim2.AnimationId = "rbxassetid://" .. Animation2
local SecondAnimation = LocalPlayer.Character.Humanoid:LoadAnimation(anim2)

--Sounds
local EatSound = script.Parent.Sounds.Eat

--Button
--local Button = player.PlayerGui:FindFirstChild("AppleGUI"):FindFirstChild("Animation2Mobile"):FindFirstChild("Button")
	          
--local Button = player.PlayerGui:WaitForChild("AppleGUI", 1).Animation2Mobile.Point

local Debounce = false
local DebounceA = false
local DebounceB = false

--Script
Tool.Equipped:connect(function()
	IdleAnimation:Play()
	GUI:Clone().Parent = player.PlayerGui
	
	
	
	
	player.PlayerGui:FindFirstChild("AppleGUI"):FindFirstChild("Animation2Mobile"):FindFirstChild("Button").MouseButton1Down:Connect(function()
		
		local Button1 = player.PlayerGui:FindFirstChild("AppleGUI"):FindFirstChild("Animation2Mobile"):FindFirstChild("Button").Top
		local Button2 = player.PlayerGui:FindFirstChild("AppleGUI"):FindFirstChild("Animation2Mobile"):FindFirstChild("Button").Bottom
		
		if DebounceA == false then
			IdleAnimation:Stop()
			SecondAnimation:Play()
			EatSound:Play()
			Button1.BackgroundColor3 = Color3.new(0.407843, 0.407843, 0.407843)
			Button2.BackgroundColor3 = Color3.new(255,255,0)

			DebounceA = true
		else
			IdleAnimation:Play()
			SecondAnimation:Stop()
			EatSound:Stop()
			Button1.BackgroundColor3 = Color3.new(255,255,0)
			Button2.BackgroundColor3 = Color3.new(0.407843, 0.407843, 0.407843)
			DebounceA = false
		end

	end)
	
	UIS.InputBegan:Connect(function(input)

		if input.KeyCode == Enum.KeyCode.F  then

			local Button1 = player.PlayerGui:FindFirstChild("AppleGUI"):WaitForChild("Animation2Mobile", 1):WaitForChild("Button", 1).Top
			local Button2 = player.PlayerGui:FindFirstChild("AppleGUI"):WaitForChild("Animation2Mobile", 1):WaitForChild("Button", 1).Bottom

			if DebounceA == false then
				IdleAnimation:Stop()
				SecondAnimation:Play()
				EatSound:Play()
				Button1.BackgroundColor3 = Color3.new(0.407843, 0.407843, 0.407843)
				Button2.BackgroundColor3 = Color3.new(255,255,0)

				DebounceA = true
			else
				IdleAnimation:Play()
				SecondAnimation:Stop()
				EatSound:Stop()
				Button1.BackgroundColor3 = Color3.new(255,255,0)
				Button2.BackgroundColor3 = Color3.new(0.407843, 0.407843, 0.407843)
				
				DebounceA = false
			end
		end
	end)

end)



Tool.Unequipped:connect(function()
	
	player.PlayerGui:WaitForChild("AppleGUI"):Destroy()
	IdleAnimation:Stop()
	SecondAnimation:Stop()
	EatSound:Stop()
	Debounce = false
	DebounceA = false
end)

Tool.Changed:Connect(function()
	if Tool.Parent == player.Backpack then
		IdleAnimation:Stop()
		SecondAnimation:Stop()
		EatSound:Stop()
	end
end)


Try removing the GUIClone when unequipping

I tried , but nothing happened. I tried also this:

	UIS.InputBegan:Connect(function(input)
		if input.KeyCode == Enum.KeyCode.F and player.PlayerGui:FindFirstChild("AppleGUI") and Status.Value == false then
				local Button1 = player.PlayerGui:FindFirstChild("AppleGUI"):FindFirstChild("Animation2Mobile"):FindFirstChild("Button").Top
				local Button2 = player.PlayerGui:FindFirstChild("AppleGUI"):FindFirstChild("Animation2Mobile"):FindFirstChild("Button").Bottom

				IdleAnimation:Stop()
				SecondAnimation:Play()
				EatSound:Play()
				Button1.BackgroundColor3 = Color3.new(0.407843, 0.407843, 0.407843)
				Button2.BackgroundColor3 = Color3.new(255,255,0)

				Status.Value = true
		end
	end)
	
	UIS.InputBegan:Connect(function(input)
		if input.KeyCode == Enum.KeyCode.F and player.PlayerGui:FindFirstChild("AppleGUI") and Status.Value == true then
			local Button1 = player.PlayerGui:FindFirstChild("AppleGUI"):FindFirstChild("Animation2Mobile"):FindFirstChild("Button").Top
			local Button2 = player.PlayerGui:FindFirstChild("AppleGUI"):FindFirstChild("Animation2Mobile"):FindFirstChild("Button").Bottom
			
			--[[IdleAnimation:Stop()
			SecondAnimation:Play()
			EatSound:Play()
			Button1.BackgroundColor3 = Color3.new(0.407843, 0.407843, 0.407843)
			Button2.BackgroundColor3 = Color3.new(255,255,0)

			Status.Value = true]]

			Button1.BackgroundColor3 = Color3.new(255,255,0)
			Button2.BackgroundColor3 = Color3.new(0.407843, 0.407843, 0.407843)
			Status.Value = false
			
			
		end
	end)

When I use one of these, all run perfectly, but when I use both, happen what I described at the start. It seems as the game don’t recognise Debounce or Status (Boolvalue that I used as sostitute of the Debounce), but it doesn’t give to me errors

1 Like