Help with Touch and TouchEndded events

Hi i am making a game and i have a part that is like a button when you stand on it your name will be in a dictionary and when you don’t touch it, it will remove you from the dictionary.
This is the code i have right now

local touched = {}

script.Parent.Touched:Connect(function(touch)
	if touch.Parent.HumanoidRootPart then
		touched[touch.Parent.Name] = 0
	end
end)

script.Parent.TouchEnded:Wait(function(touch)
	if touch.Parent.HumanoidRootPart then
		touched[touch.Parent.Name] = nil
	end
end)

This doesn’t work because when i step on to the button it does add me to the dictionary but shortly after it removes me again while i am still touching it any help?

You may be better off using Region3 since you can find all the players inside the Region3.

but how would i do that? can you give me an example

Also as a side note, wouldn’t you want to use :FindFirstChild(“HumanoidRootPart”) because the script wouldn’t error if it’s nil?

as far as i know when using something like this in an if statement it should only go the next line if it doesn’t throw an error and that the statement is true

Really? Unless I’m confusing something I think it will say “Attempt to index nil with HumanoidRootPart” but I may be wrong. You could always test and prove me wrong though :man_shrugging:

atleast it wouldn’t happen because the part will never touch a part that isn’t the player

Well I guess if nothing that doesn’t have a humanoid can touch it then you should be fine regardless.

Personally, I would use FindFirstChild just in case you later add an npc without a HumanoidRootPart.

i’ve found a solution now so i am just going to close this