Touched event firing for no reason?

I don’t really wanna dump the entirety of my code here since it’s a bit long, but in the basics:

On a part being touched, it’ll check if the player is in a certain table, if no; it’ll add the player to the table, then put them in a certain area.

Everything works fine there.

Now on a remote event; (specific one for that particular part) it will check if the request text was “leave” then remove the player from said table, then put them outside of the specific area. The issue with this, is that after around a second or so, the player gets put back inside the box with the same gui showing up that lets them leave, and they aren’t a part of the table at all, but they just get given the gui again and teleported inside.

Is there any reason why this would happen?

(specific code segment if needed; nothing substantial, PLEASE ignore the messy code, i have been modifying this code constantly in an attempt to try and fix it but nothing has been solving the issue.)

p.Touched:Connect(function(hit)
	if #tableOfPlayers < maximumAllowed then
		if players:GetPlayerFromCharacter(hit.Parent) then
			print("touched")
			local plr = players:GetPlayerFromCharacter(hit.Parent)
			local chr = plr.Character
			
			if not table.find(tableOfPlayers, plr) and not plr.PlayerGui:FindFirstChild("exitElevator") then
				if allowCountdown == true then
					startCountDown()
				end
				table.insert(tableOfPlayers, plr)
				print("added")
				chr.HumanoidRootPart.Position = p.Parent.Inside.Position
				local clone = script.exitElevator:Clone()
				clone.Parent = plr.PlayerGui
				clone.r.Disabled = false
			end
		end
		if players:GetPlayerFromCharacter(hit.Parent.Parent) then
			print("touched")
			local plr = players:GetPlayerFromCharacter(hit.Parent.Parent)
			local chr = plr.Character
			
			if not table.find(tableOfPlayers, plr) and not plr.PlayerGui:FindFirstChild("exitElevator") then
				if allowCountdown == true then
					startCountDown()
				end
				table.insert(tableOfPlayers, plr)
				print("added")
				chr.HumanoidRootPart.Position = p.Parent.Inside.Position
				local clone = script.exitElevator:Clone()
				clone.Parent = plr.PlayerGui
				clone.r.Disabled = false
			end
		end
	end
end)

script:FindFirstChild("fired").OnServerEvent:Connect(function(plr, request)
	print(request)
	if request == "leave" then
		for i, v in ipairs(tableOfPlayers) do
			if v == plr then
				table.remove(tableOfPlayers)
			end
		end
		plr.PlayerGui.exitElevator:Destroy()
		plr.Character.HumanoidRootPart.Position = p.Parent.Outside.Position
	end
end)

I don’t really want a workaround to using Touched all that much since I just want to know what’s causing it so I can avoid this in the future.

1 Like

To continue; no I was not touching the wall as I exited.

Can I ask why the script is repeated twice?

Never mind, I read the code properly.

Have you tried searching all the scripts that teleport the player using the search feature?

None of them really match to what I wanna do in my system; for more context.

This is in a box type situation; where when the player touches the wall, they get shoved inside, when the timer ends they get teleported to a game, and if they get out, they don’t get teleported. The only issue that I can find right now is that it could cause some confusion.

1 Like

Are you firing the event outside of the touched function? Because I don’t see the event being fired. Also, is the data printing in the output?

The event gets fired by a player when they want to leave the elevator, through a gui that has a button that lets them leave.

From what I’ve gathered.

  • You put them in an area
  • Then you clone the leave gui to give them permission to leave
  • if they leave then they are removed from the list and teleported out

Your issue is

  • when they are teleported out, after a few seconds they are teleported back in again (but aren’t added back to the table)

    • Correct anything that’s wrong above

I was asking to check if maybe there was another script teleporting them back inside because I don’t see any issue with this current script.

1 Like

Yes, that is exactly correct on how it works.

In a very basic test, I couldn’t replicate the error you were having. Are the part that triggers the event and the outside part very close?

wrong image

image

I’m unsure if this is fairly close but the wall on the right is the one that triggers the event.

Then I would just recommend checking if the player is in the table when the event is triggered.

Adding something like this to your code:

if table.find(tableOfPlayers, plr) then
	table.remove(tableOfPlayers, player)
end

But, I don’t know why the event would fire unless another script was calling it by accident.

Try checking if there is another script that teleports the player because I don’t see an error in the code.

There isn’t but I’m unsure if the event just doesn’t completely finish and instead waits until the player is not in the table to bring them back in.

I tried putting this in the fired event and it still has the same issue.

Nevermind, I fixed it; I apparently had the part duplicated in the model. My bad.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.