Trying to activate Gui after clicking tool

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to active a GUI when I activate a tool
  2. What is the issue? Include screenshots / videos if possible!
    It doesn’t activate the GUI when i click
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried adding a bool and a while loop to check if its true or false.
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
local tool = script.Parent
local player = game.Players.LocalPlayer
local charc = player.Character or player.CharacterAdded:wait()
local hum = charc:WaitForChild("Humanoid") 

local guiVisible = false


tool.Equipped:Connect(function()
	local idleAnim = hum:LoadAnimation(script.Parent.Animations.Idle)
	idleAnim:Play()
	local KillGui = game.ReplicatedStorage.KillGui:Clone()
	KillGui.Parent = player.PlayerGui
end)

tool.Activated:Connect(function()
	if player.PlayerGui:FindFirstChild("KillGui") then
		if player.PlayerGui.KillGui.Frame.Visible == false then
			print("Activated")
			player.PlayerGui.KillGui.Frame.Visible = true
			guiVisible = true
		elseif player.PlayerGui.KillGui.Frame.Visible == true then
			print("Activated")
			player.PlayerGui.KillGui.Frame.Visible = false
			guiVisible = true
		end
	end
end)


This is my first thread thank you

1 Like

Hi, please state what the problem is. I can see the problem with your code. Delete the clone gui from replicated storage line of script, and place the gui in startergui, and make sure the frame isn’t visible.

Try :FindFirstChild(“NameOfGui”) instead of .NameOfGui

it doesn’t activate the gui when i click

Hiwi, welcome!

Well. You are cloning a GUI from replicated storage each time that the Tool is Equipped, and you are not Destroying the last one, so it gets duplicated and adding the same GUI more times each time the tool is Equipped.

You can add a statement to check if the GUI exist in the player, or maybe destroy the gui when the tool is unequipped. I dont know what is the exact behaviour you want to achieve.

Maybe the statement?

local tool = script.Parent
local player = game.Players.LocalPlayer
local charc = player.Character or player.CharacterAdded:wait()
local hum = charc:WaitForChild("Humanoid") 

--local guiVisible = false

tool.Equipped:Connect(function()
	local idleAnim = hum:LoadAnimation(script.Parent.Animations.Idle)
	idleAnim:Play()
	
	-- If GUI exist in player, dont clone it again
	if player.PlayerGui:FindFirstChild("KillGui") then
		-- Do nothing
	else
		local KillGui = game.ReplicatedStorage.KillGui:Clone()
		KillGui.Parent = player.PlayerGui
	end
end)

tool.Activated:Connect(function()
	if player.PlayerGui:FindFirstChild("KillGui") then
		if not player.PlayerGui.KillGui.Frame.Visible then
			print("Activated")
			player.PlayerGui.KillGui.Frame.Visible = true
			--guiVisible = true
		elseif player.PlayerGui.KillGui.Frame.Visible then
			print("Activated")
			player.PlayerGui.KillGui.Frame.Visible = false
			--guiVisible = true
		end
	end
end)

While the GUI is visible and you unequip the Tool, the GUI will remain visible, thats the behaviour you want?

Not sure if your using a Handle but if your not then try disabling RequiresHandle if you haven’t yet