Trying to make gui open and close when a player touches part!

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

  1. What do you want to achieve?

A working gui open and close system!

  1. What is the issue?

The issue is its laggy when the player touches the part and sometimes the gui doesn’t show!

  1. What solutions have you tried so far?

Finding a solution!

local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()

local OpenShop = workspace.OpenShop
local gui = script.Parent

local touched = false

local function playerTouched()
	local collection = OpenShop:GetTouchingParts()
	local bodyParts = char:GetDescendants()

	for _,bodyPart in pairs(bodyParts) do
		if table.find(collection , bodyPart) then
			return true
		end
	end

	return false
end

OpenShop.Touched:Connect(function()
	if playerTouched() then
		if script.Parent.Open.Value == false and script.Parent.Frame.Position == UDim2.new(0.5, 0,-0.5, 0) then
			gui.Frame:TweenPosition(UDim2.new(0.5, 0,0.5, 0),Enum.EasingDirection.In,Enum.EasingStyle.Linear,0.2)
			script.Parent.Open.Value = true
		end
	end
end)

OpenShop.TouchEnded:Connect(function()
	if not playerTouched() then
		if script.Parent.Open.Value == true and script.Parent.Frame.Position == UDim2.new(0.5, 0,0.5, 0) then
			gui.Frame:TweenPosition(UDim2.new(0.5, 0,-0.5, 0),Enum.EasingDirection.In,Enum.EasingStyle.Linear,0.2)
			script.Parent.Open.Value = false
		end
	end
end)

Thank you in advance!

Instead of using GetTouchingParts() you can just get the touching part from the .Touched:Connect() function.

I’d maybe do something like this.

local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()

local OpenShop = workspace.OpenShop
local gui = script.Parent

-- get the touching object from the connection
OpenShop.Touched:Connect(function(hit)
	--check if it was one of the player characters parts
	if hit.Parent == char  then		
		--check if the gui is already displayed
		if gui.Frame.Visible == false then
			--show the gui
			gui.Frame.Visible = true
			gui.Frame:TweenPosition(UDim2.new(0.5, 0,0.5, 0),Enum.EasingDirection.In,Enum.EasingStyle.Linear,0.2)
		end
	end
end)

(as a note, I’ve tried using a similar way of opening Gui, you may run into problems when the player closes the Gui, and then moves - only to be hit with the Gui again, and again.)

1 Like
local Players = game:GetService("Players")

local player = Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()

local OpenShop = workspace.OpenShop
local gui = script.Parent

OpenShop.Touched:Connect(function(hit)
   if not Players:GetPlayerFromCharacter(hit.Parent) then return end
   if script.Parent.Open.Value == false and script.Parent.Frame.Position == UDim2.new(0.5, 0,-0.5, 0) then
      gui.Frame:TweenPosition(UDim2.new(0.5, 0,0.5,0),Enum.EasingDirection.In,Enum.EasingStyle.Linear,0.2)
	end
end)

I forgot to post the full script!

local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()

local OpenShop = workspace.OpenShop
local gui = script.Parent

local touched = false

local function playerTouched()
	local collection = OpenShop:GetTouchingParts()
	local bodyParts = char:GetDescendants()

	for _,bodyPart in pairs(bodyParts) do
		if table.find(collection , bodyPart) then
			return true
		end
	end

	return false
end

OpenShop.Touched:Connect(function()
	if playerTouched() then
		if script.Parent.Open.Value == false and script.Parent.Frame.Position == UDim2.new(0.5, 0,-0.5, 0) then
			gui.Frame:TweenPosition(UDim2.new(0.5, 0,0.5, 0),Enum.EasingDirection.In,Enum.EasingStyle.Linear,0.2)
			script.Parent.Open.Value = true
		end
	end
end)

OpenShop.TouchEnded:Connect(function()
	if not playerTouched() then
		if script.Parent.Open.Value == true and script.Parent.Frame.Position == UDim2.new(0.5, 0,0.5, 0) then
			gui.Frame:TweenPosition(UDim2.new(0.5, 0,-0.5, 0),Enum.EasingDirection.In,Enum.EasingStyle.Linear,0.2)
			script.Parent.Open.Value = false
		end
	end
end)

It keeps lagging when I stand on the part while the gui is visible.

Maybe if you try something like this?

local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()

local OpenShop = workspace.OpenShop
local gui = script.Parent

local touched = false

OpenShop.Touched:Connect(function()
	local humanoid = OpenShop:FindFirstChildWhichIsA("Humanoid")
	if humanoid then
		if script.Parent.Open.Value == false and script.Parent.Frame.Position == UDim2.new(0.5, 0,-0.5, 0) then
			gui.Frame:TweenPosition(UDim2.new(0.5, 0,0.5, 0),Enum.EasingDirection.In,Enum.EasingStyle.Linear,0.2)
			script.Parent.Open.Value = true
		end
	end
end)

OpenShop.TouchEnded:Connect(function()
	local humanoid = OpenShop:FindFirstChildWhichIsA("Humanoid")
	if humanoid then
		if script.Parent.Open.Value == true and script.Parent.Frame.Position == UDim2.new(0.5, 0,0.5, 0) then
			gui.Frame:TweenPosition(UDim2.new(0.5, 0,-0.5, 0),Enum.EasingDirection.In,Enum.EasingStyle.Linear,0.2)
			script.Parent.Open.Value = false
		end
	end
end)