How would I make this gui disappear when the player steps of the part?

Hi there, I have a gui part script that shows the gui when the part is touched. What I’m trying to achieve is to make it so that when it’s not touching the part, the gui disappears. I’ve tried using touchended however that didn’t seem to work.

local gui = script.Parent.Parent.Parent.GUI
local button = script.Parent
local part = workspace.Shop.Model.GUIPart
local shop = part.GUIPart

gui.Visible = true
gui.Position = UDim2.new(0.5,0,1.302,0)

button.MouseButton1Click:Connect(function()
	workspace.clickeffect:Play()
	if gui.Position == UDim2.new(0.5,0,1.302,0) then
		gui:TweenPosition(
			UDim2.new(0.5,0,0.5,0),

			"Out",
			"Sine",
			.5,
			false
		)	
	else
		gui:TweenPosition(
			UDim2.new(0.5,0,1.302,0),

			"Out",
			"Sine",
			.5,
			false
		)	
	end	

end)

shop.Touched:Connect(function()
	if gui.Position == UDim2.new(0.5,0,1.302,0) then
		gui:TweenPosition(
			UDim2.new(0.5,0,0.5,0),

			"Out",
			"Sine",
			.5,
			false
		)	
	else
		gui:TweenPosition(
			UDim2.new(0.5,0,1.302,0),

			"Out",
			"Sine",
			.5,
			false
		)	
	end	

end)

local function TouchEnded(hit)
	if (hit.Parent:findFirstChild("Humanoid") ~= nil) then
		gui:TweenPosition(
			UDim2.new(0.5,0,1.302,0),

			"Out",
			"Sine",
			.5,
			false
		)	
	end
end

That’s a different function. I’m talking about the one at the bottom of the script. Please read the topic carefully and you should know where I’m talking about :slight_smile:

Why did you define it as Humanoid ~= nil if the function “TouchEnded” Should do that instead of you.

Just in case it, I know it’s not necessary.

It’s not unnecessary it’s literally ruins the function tries to remove the ~= nil

Alright, I tried removing that line but it still doesn’t disappear.

Perhaps try to make an event connected to client script in the GUI and try it

both functions (touched and touchended)?

I’ll elaborate, you can’t make gui command line through this hit function so it won’t help you.

If the touched is working and through MouseButton1Click then it should work perfectly fine unless it’s through server script therefore try what I’ve wrote in the first comment

I did try that, I added a new function for touchended and it still doesn’t work. I’ll try changing touchended with a remote function in serverscript and then fire it to the client/local script.

That’s what I said just before

I don’t know what is wrong, but it still doesn’t work. I’ll send you the script.

remote handler

local part = workspace.Shop.Model.GUIPart
local shop = part.GUIPart

shop.Touched:Connect(function(hit)
	if game.Players:GetPlayerFromCharacter(hit.Parent) then
		game.ReplicatedStorage.hidegui:FireClient(game.Players:GetPlayerFromCharacter(hit.Parent))
	end
end)

client script

local gui = script.Parent.Parent.Parent.GUI
local button = script.Parent
local part = workspace.Shop.Model.GUIPart
local shop = part.GUIPart

gui.Visible = true
gui.Position = UDim2.new(0.5,0,1.302,0)

button.MouseButton1Click:Connect(function()
	workspace.clickeffect:Play()
	if gui.Position == UDim2.new(0.5,0,1.302,0) then
		gui:TweenPosition(
			UDim2.new(0.5,0,0.5,0),

			"Out",
			"Sine",
			.5,
			false
		)	
	else
		gui:TweenPosition(
			UDim2.new(0.5,0,1.302,0),

			"Out",
			"Sine",
			.5,
			false
		)	
	end	

end)

shop.Touched:Connect(function()
	if gui.Position == UDim2.new(0.5,0,1.302,0) then
		gui:TweenPosition(
			UDim2.new(0.5,0,0.5,0),

			"Out",
			"Sine",
			.5,
			false
		)	
	else
		gui:TweenPosition(
			UDim2.new(0.5,0,1.302,0),

			"Out",
			"Sine",
			.5,
			false
		)	
	end	

end)

game.ReplicatedStorage.hidegui.OnClientEvent:Connect(function()
	
		gui:TweenPosition(
			UDim2.new(0.5,0,1.302,0),

			"Out",
			"Sine",
			.5,
			false
		)	
end)

Well I just fixed it using debounce and keeping touched only instead of having touchedended too.

Serverscript (remote handler)

local part = workspace.Shop.Model.GUIPart
local shop = part
local debounce = false

shop.Touched:Connect(function(hit)
	if not debounce then
		debounce = true
		if game.Players:GetPlayerFromCharacter(hit.Parent) then
			game.ReplicatedStorage.showgui:FireClient(game.Players:GetPlayerFromCharacter(hit.Parent))
		end
		wait(2)
		debounce = false
	end
end)

You could’ve added a yield to the callback function connected to the TouchEnded function, that way it’ll only execute once that yield has expired.

local function TouchEnded(hit)
	if (hit.Parent:findFirstChild("Humanoid") ~= nil) then
		task.wait(5)
		gui:TweenPosition(
			UDim2.new(0.5,0,1.302,0),

			"Out",
			"Sine",
			.5,
			false
		)	
	end
end

This would wait 5 seconds before performing the tween, the Touched event would need a small debounce too to prevent it from executing the code within its connected callback function too frequently.