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

How to make a ClickDetector unclickable by other players while it is in use?

So, I'm making a toggle-able ClickDetector script, where the player who clicks the part will start doing x. Once they click the part again, they will no longer be doing x.

I need to know how I can make no other player be able to do x while a player is already doing it.

local chat = game:GetService('Chat')
local Players = game:GetService('Players')
local talker1 = workspace.talker1.ClickDetector
local talker2 = workspace.talker2.ClickDetector
local vacant = workspace.talker1.boolValue

local repStorage = game:GetService('ReplicatedStorage')
local toggleMovement = repStorage:WaitForChild('toggleMovement')

talker1.MouseClick:Connect(function(playerWhoClicked)
	
	if talker1.Parent.BrickColor == BrickColor.Red() then
		talker1.Parent.BrickColor = BrickColor.Black()
	else
		talker1.Parent.BrickColor = BrickColor.Red()
	end
	
	toggleMovement:FireClient(playerWhoClicked)
			
	local findBoolOne = playerWhoClicked.Character.Humanoid:FindFirstChild('talkerBoolOne')
	Player = playerWhoClicked
	
	if not findBoolOne then
		local talkerBoolOne = Instance.new("BoolValue", playerWhoClicked.Character.Humanoid)
		talkerBoolOne.Name = "talkerBoolOne"
	end
	
	
	talkerOne = playerWhoClicked.Character.Humanoid:FindFirstChild('talkerBoolOne')
			
	playerWhoClicked.Character.Humanoid.Died:Connect(function()
		talkerOne.Value = false
	end)
			
	if talkerOne.Value == false then
		talkerOne.Value = true
	else
		talkerOne.Value = false
	end
	
end)

1 comment

0

add a debounce

1 answer

When a player clicks the clickdetector, set a debounce to true and check for it in the beginning of the function. Once you want another player to click it, you can set the debounce to false so the function can continue as normal.

Example:

local debounce

ClickDetector.MouseClick:Connect(function(player)
	if debounce then return end
	if game:GetService("Players")[player.Name] then -- checks for the player
		debounce = true
	end
	-- your code here
	-- once you want another player to click the clickdetector set the denounce to false 
end)
	
	

4 comments

0

this script is wrong

0

you really shouldn't be using local variables outside of their scope

0

there's nothing wrong with his script and it's a correct way to do it.

0

You don't need to do that check on line 5. And the way you're indexing the player is stupid. But MCAndRobloxUnited, I don't see your point.

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