How to stop touching a part from too many times?

I wanted make a part when touched it teleports to a different place. But here’s a problem, when the part is touched too many times, the game freezes and crash. I tried to find a way to stop it by making any player who touches it can have the affect for one time. But, I don’t know how to do it myself.

local Main = game.Workspace.Maps.TeleportMagic1
local Floors = game.Workspace.Maps.Floor1
local Place = game.Workspace.Maps.Walls1
local Location = game.Workspace.Maps.NameTag1.BillboardGui.PlayerCounter1
local PlaceID = ----
local Teleport = game:GetService("TeleportService")
local JumpServ = script.Parent.Parent.JumpService.Jump1

while true do
	wait(0.1)
	if Location.Text == "7/10" then
		script.Parent.Parent.VoteAmount1.Disabled = true
		Location.Text = "Teleporting"
		for i, v in pairs(Place:GetChildren()) do
			v.CanCollide = true
		end
		wait(0.1)
		Main.Touched:Connect(function(hit) -- Detect the player, teleports to the game.
			local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
			if Player then
				Teleport:Teleport(PlaceID, Player)
				if Main.Touched:Connect() then
					
				end
			end
		end)
		JumpServ.Disabled = false
		Floors.Velocity = Vector3.new(0,75,0)
		wait(10)
		JumpServ.Disabled = true
		Floors.Velocity = Vector3.new(0,0,0)
		script.Parent.Parent.VoteAmount1.Disabled = false
		for i, v in pairs(Place:GetChildren()) do
			v.CanCollide = false
		end
	end
end

You have to use debounce for that.

1 Like

just use debounce so it has a cooldown when u touch it

1 Like

That’s a good idea! Is it the one like

local Debouce = false

This is an example code of how Debounce works:

local Debounce = false

Part.Touched:Connect(function(Hit)
    if not Debounce then -- Code inside this statement will not run unless Debounce = false
        Debounce = true -- This will set Debounce to true, so the if statement will block off anymore requests
        -- Run code
        wait(5) -- or however long it takes until debounce shall change to false again
        Debouce = false
    end
end)
4 Likes