.Touched can only get triggered once

When I ever a touch a certain part, it opens and I can also close it. But when I try to touch the part again, nothing happens.

This may be a problem with the close script but something tells me it is the touch script. Also there is no errors

Open script:

local UpgradePart = script.Parent

UpgradePart.Touched:Connect(function(hit)
	local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if Player then
		local PlrShop = Player.PlayerGui.UpgradesShop
		PlrShop.MainFrame.Visible = true
	end
end)

Close script (local script):

function close(name)
	game.Players:FindFirstChild(name).PlayerGui.UpgradesShop.MainFrame.Visible = false
end

--PC
script.Parent.MouseButton1Click:Connect(function()
	close(game.Players.LocalPlayer.Name)
end)

--Mobile
script.Parent.TouchTap:Connect(function()
	close(game.Players.LocalPlayer.Name)
end)
2 Likes

This is because you are setting the UI to be visible through a server script and invisible through a local script. The server doesn’t recognize that it isn’t visible and therefore doesn’t change anything.

2 Likes

I agree with the above, the best way to make it work 100% is to use remote events. Whenever the player touches the part, have the remote event fired by the server script to the client using game.Players:GetPlayerFromCharacter() as the first parameter, and have a local script catch the event and open the gui to the player. Straight forward, right? This should work 100% of the time. Since the close script is a local script it doesnt need to be altered.

Hopefully this helps you further with your issue. Make sure to mark as a solution if this was the right answer for you.

this is also my first post ever on the devforum!

2 Likes

As has been stated, this is occurring because you’re setting a property’s value from the client which means that this change won’t replicate to the server, thus when you touch the part again, from the server’s perspective the gui is already open (and attempting to set the “Visible” property’s value to true will do nothing).

There’s a couple of fixes for this, you can either use a RemoteEvent instance, or you can quickly toggle the property’s value from false back to true, or you could additionally make use of the BasePart instances “.TouchEnded” event to close the gui instead of a close button.

TouchEnded event implementation:

local UpgradePart = script.Parent
local Touching = false

UpgradePart.Touched:Connect(function(hit)
	if Touching then
		return
	end
	
	local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if Player then
		Touching = true
		local PlrShop = Player.PlayerGui.UpgradesShop
		PlrShop.MainFrame.Visible = true
	end
end)

UpgradePart.TouchEnded:Connect(function(hit)
	if not Touching then
		return
	end
	
	local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if Player then
		Touching = false
		local PlrShop = Player.PlayerGui.UpgradesShop
		task.wait(3)
		if not Touching then
			PlrShop.MainFrame.Visible = false
		end
	end
end)

Toggling the property’s value implementation:

local UpgradePart = script.Parent
local Touching = false

UpgradePart.Touched:Connect(function(hit)
	if Touching then
		return
	end
	
	local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if Player then
		Touching = true
		local PlrShop = Player.PlayerGui.UpgradesShop
		PlrShop.MainFrame.Visible = false
		PlrShop.MainFrame.Visible = true
	end
end)

I didn’t write an implementation which involves the use of a RemoteEvent instance but if you’d like one, let me know.

2 Likes