part.Touched calling twice, even with a debounce

I am trying to duplicate a room only on the client, and I already have that down.The issue is that the detector (which detects the play with part.Touched) calls twice, even though we have a variable that should stop that.

(Video of the issue)
robloxapp-20200626-1904282.wmv (950.7 KB)

Edit: Realised that I uploaded a download of the video, rather than embedding it. Is it possible to embed it instead as I understand people may not be comfortable with downloading anything?
out:
image

I have tried re-enabling the variable after to see if I was re-enabling to quickly, but that does not fix the issue. This is ran in a server script, and I am stuck on what to do to fix it. I imagine it is something stupid that I have done, but I do not know. It should also be noted that it is the touched event being called twice, not me calling the function by mistake.

Code:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local Players = game:GetService("Players")

local Remotes = ReplicatedStorage:WaitForChild("Remotes")
local Modules = ReplicatedStorage:WaitForChild("Modules")

local GetDataServer = ServerStorage.Bindables.GetDataServer
local ChangeValue = ServerStorage.Bindables.ChangeValue
local ClientDuplication = Remotes:WaitForChild("ClientDuplication")
local ClientPropertyAssignment = Remotes:WaitForChild("ClientPropertyAssignment")
local DefaultData = require(Modules:WaitForChild("DefaultData"))

local part = script.Parent 
local tpLocation = script.TPLocation.Value.Position
local defaultOffice = script.Target.Value
local clientDir = workspace.ClientRooms
local windowsTransparencyLevel = 0.8
local office = workspace.Office
local playerWindows = office.PlayerWindows
local canEnter = true

function PropertyWindows(player, dir)
	for _, window in pairs(dir:GetChildren()) do
		ClientPropertyAssignment:FireClient(player, window, "Transparency", windowsTransparencyLevel)
		
	end
	
end

part.Touched:Connect(function(collider)
	if not collider or not collider.Parent:FindFirstChild("Humanoid") or not canEnter then return end
	canEnter = false;
	print(collider.Name)

	local character = collider.Parent
	local localPlayer = Players:FindFirstChild(character.Name)
	local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
	
	if not localPlayer or not humanoidRootPart then canEnter = true; return end
	
	local hasOffice = GetDataServer:Invoke(localPlayer, DefaultData.ownerships.slot, "defaultOffice")

	if not hasOffice then canEnter = true; return end

	ChangeValue:Fire(localPlayer, DefaultData.primary.slot, "inHouseWith", localPlayer.UserId)
	
	ClientDuplication:FireClient(localPlayer, defaultOffice, clientDir, tostring(localPlayer.UserId) .. defaultOffice.Name)
	
	wait(1)
	humanoidRootPart.Position = tpLocation
	
	PropertyWindows(localPlayer, playerWindows)
	
	for _, player in pairs(Players:GetPlayers()) do
		if player.UserId == localPlayer.UserId then continue end
		
		local inHouseWithPlayer = GetDataServer:Invoke(player, DefaultData.primary.slot, "inHouseWith") == localPlayer.UserId
		
		if inHouseWithPlayer then continue end
		
		for _, part in pairs(character:GetChildren()) do
			if part:IsA("BasePart") then
				ClientPropertyAssignment:FireClient(player, part, "Transparency", 1)
				
			end
			
		end
		
	end
		
	canEnter = true
	
	
end)
1 Like

I assume the hit Event runs twice before canEnter is set to false.

This is most likely the issue, but I have no idea how to fix it. It is the second line of the function. Any suggestions?

Try doing this?

if not canEnter or not collider --(etc, continue with what you had before)
if canEnter and collider.Parent:FindFirstChild("Humanoid") then

How can I write chunks of code in a thread? o.O

It should be not canEnter since he is setting it to false as I showed above.

You are both wrong and correct.
My conditionals are to lock the whole if statement under those conditions.

Yours are to return and not run the code.

Just tried that, but it didnt work. I have no idea why. I have used similar code before and it worked. :frowning:

you use triple graves (`) at the beginning and end.

How do you mark as spoiler?

Does the function call twice simultaneously, or is there a delay? I noticed a print statement between both the first part being touched vs the second.

It seems that the second clone waits for the first to finish, which makes no sense to me at it is an event.

Solved! I fixed the issue by changing it from a boolean to a variable that stores tick() + xAmountOfTime. I just check if tick() - that variable is > 0, and this solved the issue.

Chunck of code I changed:

local enterTime = tick() + waitTime 

part.Touched:Connect(function(collider)
	if not collider.Parent:FindFirstChild("Humanoid") then return end
	
	if tick() - enterTime > 0  then
		enterTime = tick() + waitTime
2 Likes