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

Player Hits Part To Many Times?

Hello,

In my game there is a portal that when you step into it, it gives you a point (referred to as "Ladder Points") and then teleports you away, but in the same second, the player hits the portal 5-10 times which I do not want. Any ideas on how to solve this?

Here is my current script:

local PortalPart = script.Parent
local Spawn = game.Workspace.Spawn
 
local function onPartTouched(hit)
	local PlayerName = hit.Parent.Name
	local player = game.Players:FindFirstChild(PlayerName)
	local LadderPoints = player.leaderstats:FindFirstChild("Ladder Points")
	if player then
		player.Character:MoveTo(Spawn.Position)
		LadderPoints.Value += 1
	end
end

PortalPart.Touched:Connect(onPartTouched)

Thanks In Advance!

2 answers

✓ Accepted answer

Hello there!

Because of how the Roblox engine functions, there is a system named Debounce. Debounce allows the part to be prevented from being hit too many times at once. https://developer.roblox.com/en-us/articles/Debounce

It is quite simple to insert debounce in a script that is used for a part, and this is how you would do it:

local PortalPart = script.Parent
local Spawn = game.Workspace.Spawn
local Debounce = false

local function onPartTouched(hit)
   if not Debounce then
    local PlayerName = hit.Parent.Name
    local player = game.Players:FindFirstChild(PlayerName)
    local LadderPoints = player.leaderstats:FindFirstChild("Ladder Points")
    if player then
          Debounce = true
        player.Character:MoveTo(Spawn.Position)
        wait(0.1)
        LadderPoints.Value += 1
      Debounce = false
    end
end
end

PortalPart.Touched:Connect(onPartTouched)

Try it out!

2 comments

0

Thank you for you reply, works perfectly! By the way, I think you forgot an "end" at the end, 1 for the function, and 2 for the if statements.

0

Oh yeah, sorry for that! If you were able to fix it, feel free to accept my answer!

You could just add a cooldown (debounce). https://developer.roblox.com/en-us/articles/Debounce

When the cooldown is false, you can hit it no problem, but after you hit it will become true, and you'll have to wait a certain amount of time.

local PortalPart = script.Parent
local Spawn = game.Workspace.Spawn
local cooldown = false
local function onPartTouched(hit)
    if cooldown == false then
        local PlayerName = hit.Parent.Name
        local player = game.Players:FindFirstChild(PlayerName)
        local LadderPoints = player.leaderstats:FindFirstChild("Ladder Points")
        if player then
            player.Character:MoveTo(Spawn.Position)
            LadderPoints.Value += 1
            cooldown = true
            wait(3) -- time for the cooldown to end
            cooldown = false
        end
    end
end

PortalPart.Touched:Connect(onPartTouched)

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