Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to check if a player has already touched something once?

Is there a way to check if a player has touched something once and then shut it down for only that player?

1 comment

0

Yes you would need to list what players have touched the item. Then check this list each time the touched event is fired

1 answer

Like kingdom5 said, you could do this by creating a dictionary of people who have touched the part, to recognize if the player has touched it before. This would basically just act as a one-way debounce method for each player. Here's an example:

-- Players service
local players = game:GetService("Players")

-- dictionary of people who touched the part
local touched = {}

-- Assuming the script is inside a part, the following
-- applies.
script.Parent.Touched:connect(function(hit)
	-- Get the player from the character
	local user = players:GetPlayerFromCharacter(hit.Parent)

	-- If the player exists, then...
	if user then

		-- Check to see if the user exists in the dictionary
		if touched[user] then
			print("Player has already touched this part")
			return -- If it does, return to discontinue.
		else
			-- If the player doesn't exist in the table, then
			-- touched[user] = true adds them.
			print("Player has touched the part for the first time")
			touched[user] = true
		end

		-- This will only fire once per player, due to our
		-- return instruction if the player exists in the table
		print("Part was touched")

	end
end)

Hope that helped, let me know if you have any questions.

This discussion is preserved as it was. New answers, comments, and votes are closed.