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)
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)
Hope this helps, and I appreciate being marked as solution if this was your solution.
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