Clickdetector don't work when its clicked by 2 players at the same time

Hello, i just finished making my egg hatching system but i noticed that when more than 1 player click the egg dispenser at the same time it wont work he has to wait until the other player have finished hatching it here is my script:

local clickdetector = script.Parent.ClickDetector
local cd = false
local cost = 40
local module = require(script.Parent:WaitForChild("ModuleScript"))
local pets = script.Parent.Parent.PetFolder
clickdetector.MouseClick:Connect(function(player)
	
	local pg = player:WaitForChild("PlayerGui")
	local inv = pg.Inventory
	local egg = script.Parent.Parent.ViewportFrame:Clone()
	local pet = script.Parent.Parent.ViewportFrame2:Clone()
	if player.leaderstats.Coins.Value >= cost then
			if not cd then
			cd = true
			player.leaderstats.Coins.Value = player.leaderstats.Coins.Value - cost
			local p = module.randPet()
			p:Clone().Parent = player.Pets
			egg.Parent = inv
			wait(2)
			egg:Destroy()
			pet.Parent = inv
			local obj = p:Clone()
			obj.Parent = pet
			local cam = Instance.new("Camera")
			cam.CFrame = CFrame.new(obj.PrimaryPart.Position + (obj.PrimaryPart.CFrame.LookVector * 3), obj.PrimaryPart.Position)
			cam.Parent = script.Parent
			pet.CurrentCamera = cam
			wait(1.5)
			pet:Destroy()
			wait(0)
			cd = false
		end
	end
end)

Thank you!

2 Likes

So my idea is making a bool value inside player and checking if its set to true, so other players will not need to wait.

local clickdetector = script.Parent.ClickDetector
local cd = false
local cost = 40
local module = require(script.Parent:WaitForChild("ModuleScript"))
local pets = script.Parent.Parent.PetFolder
clickdetector.MouseClick:Connect(function(player)
if not player:FindFirstChild("CanOpenEgg") then
	local canOpenEgg = Instance.new("BoolValue")
	canOpenEgg.Parent = player
	canOpenEgg.Name = "CanOpenEgg"
	canOpenEgg.Value = true
end
	local pg = player:WaitForChild("PlayerGui")
	local inv = pg.Inventory
	local egg = script.Parent.Parent.ViewportFrame:Clone()
	local pet = script.Parent.Parent.ViewportFrame2:Clone()
	if player.leaderstats.Coins.Value >= cost then
			if player:FindFirstChild("CanOpenEgg").Value == true then
			player:FindFirstChild("CanOpenEgg").Value = false
			player.leaderstats.Coins.Value = player.leaderstats.Coins.Value - cost
			local p = module.randPet()
			p:Clone().Parent = player.Pets
			egg.Parent = inv
			wait(2)
			egg:Destroy()
			pet.Parent = inv
			local obj = p:Clone()
			obj.Parent = pet
			local cam = Instance.new("Camera")
			cam.CFrame = CFrame.new(obj.PrimaryPart.Position + (obj.PrimaryPart.CFrame.LookVector * 3), obj.PrimaryPart.Position)
			cam.Parent = script.Parent
			pet.CurrentCamera = cam
			wait(1.5)
			pet:Destroy()
			wait(0)
			player:FindFirstChild("CanOpenEgg").Value = true
		end
	end
end)
1 Like

Thank you i will try doing that!

2 Likes

If that don’t work, use :WaitForChild()

2 Likes

Okay thank you! (30 characters)

1 Like

@xaoix00, I find that bool values can lead to inconsistencies. I tend to use a table full of players that are on debounce.

Try something like this, @polarisprog.

Hope this helps, and I appreciate being marked as solution if this was your solution. :heart:

local debouncePlrs = {}

function clicked(plr)
 if not table.find(debouncePlrs, plr) then -- if the player is not in the debounce table
  table.insert(debouncePlrs, plr) -- put them into the debounce table
  -- Handle the egg here
  table.remove(debouncePlrs, table.find(debouncePlrs, plr)) -- remove them from debounce
 end
end
2 Likes

I thinked about it, but I don’t really understand tables…

1 Like

Tank you! (30 characterssssss)

1 Like

That’s fine! If you are unsure on the correct usage of arrays, you might find these wiki articles somewhat useful:

1 Like

Ok thanks for help (30 characters)

1 Like

Ok thanks for solution, but I think @Winky_y’s script will work better

0:<
Why didn’t I get the solution… lmao.

2 Likes

It actually didn’t work lol. (30 characters)

Check table | Documentation - Roblox Creator Hub

Also @Winky_y didn’t run the function, type

clickdetector.MouseClick:Connect(clicked)

Did you apply it to your script appropriately? Were there any errors?

1 Like