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

How to stop multiple touches from occurring?

I have a script that teleports you and clones a GUI into the player when the player touches a part. However, theres always multiple GUI's being cloned. I've tried to stop this with a debounce, but it wouldn't work. I'm not sure if I'm doing something wrong.

local seats = game.Workspace.room1.seats:GetChildren()
local exitgui = script.Parent.exitgui
local debounce = false

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		if debounce == false then
			debounce = true
			local player = game.Players:GetPlayerFromCharacter(hit.Parent)
			for i,v in pairs(seats) do
				if seats.Occupant == nil then
					hit.Parent.Humanoid.JumpPower = 0
					hit.Parent.Humanoid.WalkSpeed = 0
					script.Parent.slam:Play()
					exitgui:Clone().Parent = player.PlayerGui
					player.Character.HumanoidRootPart.CFrame = seats[math.random(1,10)].CFrame 
				end
			end
		end
	end
end)		

1 comment

0

You need to change debounce back to false before you end.

2 answers

You forgot to set the debounce to false

local seats = game.Workspace.room1.seats:GetChildren()
local exitgui = script.Parent.exitgui
local debounce = false

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        if debounce == false then
            debounce = true
            local player = game.Players:GetPlayerFromCharacter(hit.Parent)
            for i,v in pairs(seats) do
                if seats.Occupant == nil then
                    hit.Parent.Humanoid.JumpPower = 0
                    hit.Parent.Humanoid.WalkSpeed = 0
                    script.Parent.slam:Play()
                    exitgui:Clone().Parent = player.PlayerGui
                    player.Character.HumanoidRootPart.CFrame = seats[math.random(1,10)].CFrame 
			wait(1)
			debounce = false
                end
            end
        end
    end
end)        

8 comments

1

well, even if i didn't set the debounce to false, wouldn't that still mean that the touch will be disabled if i touch it?

0

Big brain time

0

Debounce doesn't affect whether or not the touch event will work. It just keeps players from spamming if you code it right. Don't worry, Nguyen demonstrates it correctly in his script.

0

Thanks @LoveSpeaksOutTruth, somehow i didn't get the notification 

0

i have added the debounce = false. however, after every second, the command repeats

0

Isn't what it supposed to do ? It stops spamming 

0

it still spams, except it spams slower?

0

Yep every 1 sec

There is quite simple solution for that. Instead of having debounce you put line that checks if there is already a gui inside of player. So it will look like this:

local seats = game.Workspace.room1.seats:GetChildren()
local exitgui = script.Parent.exitgui
 
script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
            local player = game.Players:GetPlayerFromCharacter(hit.Parent)
            for i,v in pairs(seats) do
                if seats.Occupant == nil then
                    hit.Parent.Humanoid.JumpPower = 0
                    hit.Parent.Humanoid.WalkSpeed = 0
                    script.Parent.slam:Play()
						  if player.PlayerGui:FindFirstChild"exitgui" then
							player.Character.HumanoidRootPart.CFrame = seats[math.random(1,10)].CFrame
						else
                    exitgui:Clone().Parent = player.PlayerGui
                    player.Character.HumanoidRootPart.CFrame = seats[math.random(1,10)].CFrame
						end
               end
           end
      end
end) 

3 comments

0

it works the first time, but then it doesn't clone the gui if i try it again

0

it works the first time, but then it doesn't clone the gui if i try it again

0

I assume that the exit gui is close button right? If yes then when you click it, make sure it destroys the gui and does not just make it unvisible.

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