Code issues - The GUI does not activate properly

Hello! I’m trying to make sure that when a player touches the part a specific gui is activated for the local player, what am I doing wrong? currently the gui is not shown or hidden…

image

local part = script.Parent
local playerGuiName = "Cooking"

local function activatePlayerGui(player)
	-- Try to find the PlayerGui in StarterGui
	local starterGui = player:FindFirstChild("StarterGui")
	if starterGui then
		local playerGui = starterGui:FindFirstChild(playerGuiName)
		if playerGui then
			playerGui.Enabled = true
			print("PlayerGui activated for player: " .. player.Name)
			return playerGui
		end
	end

	warn("Could not find PlayerGui '" .. playerGuiName .. "' in StarterGui for player: " .. player.Name)
	return nil
end

local function deactivatePlayerGui(playerGui)
	if playerGui then
		playerGui.Enabled = false
		print("PlayerGui deactivated")
	end
end

part.Touched:Connect(function(hit)
	local character = hit.Parent

	local humanoid = character:FindFirstChild("Humanoid")
	if humanoid then
		-- Print debug information
		print("Touched event triggered for player: " .. character.Name)

		-- Activate the PlayerGui for the player
		local playerGui = activatePlayerGui(character)

		-- Connect TouchEnded to deactivate PlayerGui
		local connection
		connection = part.TouchEnded:Connect(function()
			print("TouchEnded event triggered for player: " .. character.Name)
			deactivatePlayerGui(playerGui)

			-- Disconnect the connection after use
			connection:Disconnect()
		end)
	end
end)
1 Like

In the activatePlayerGui, it seems you are giving the function the character instead of player. Use game.Players.GetPlayerFromCharacter to get the actual player.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.