Egg Hatching System problems

Hello!
I want to create my own egg hatching system and my own simulator with that system in, but, there has occured some bugs.

Let me explain the plan of the system:

  1. purchase gui trigger
  2. triggering main purchase script in ServerScriptService with sending some information like level
  3. check if level >= requiring level
  4. if check has completed successfully, system will trigger main purchase module
  5. module’s randomizer triggered
  6. return randomized choice of main purchase module
  7. (for now) printing pet’s name

So, you might ask me:
What is the problem?

There it is:
bug screenshot
Purchase system is saying pet’s names twice or three times in a row without a delay.

First line is saying that everything is normal and pet’s name printed only once.
Second is saying “hey dude you pressed three times without a delay hahahaha live with it” and its repeating.

I tried to put cooldown in the gui, in the purchase script, but nothing helped me.

Gui script:

local text = script.Parent.Main
local yes = script.Parent.bg_yes.Yes
local no = script.Parent.bg_no.No

local rep = game:GetService('ReplicatedStorage')
local event = rep.ClientEvents.PurchaseGuiCall
local event2 = rep.FCTSEvents.PurchaseCall

local types = {
	['Classic'] = {
		['LvlReq'] = 1;
		['Cost'] = 150;
		['Text'] = "Are you sure you want to buy the Classic sphere for 150$?";
	};
}

local plr = game.Players.LocalPlayer

local deb = false

event.OnClientEvent:Connect(function(callType)
	if deb == false then
		deb = true
		local ct = types[callType]
		script.Parent.Visible = true
		if ct ~= nil then
			text.Text = ct.Text
			yes.MouseButton1Click:Connect(function()
				event2:FireServer({pt = 'Spheres',sphere='classic',lvl=ct.LvlReq,cost=ct.Cost})
				script.Parent.Visible = false
			end)
			no.MouseButton1Click:Connect(function()
				script.Parent.Visible = false
			end)
		else
			script.Parent.Visible = false		
		end
		wait(1)
		deb = false
	end
end)

Purchase script:

local rep = game:GetService('ReplicatedStorage')
local events = rep.FCTSEvents
local event = events.PurchaseCall

local ss = game:GetService('ServerScriptService')
local modules = ss.modules
local noobsgiver = modules.noobsgiver

event.OnServerEvent:Connect(function(plr,info)
	local purchasetype = info.pt
	if purchasetype == 'Spheres' then
		local sph = info.sphere
		local cost = info.cost
		local lvl = info.lvl
					
		wait(0.01)
		local value = plr.leaderstats.Cash
		local value2 = plr.leaderstats.Level
				
		if value2.Value < lvl then
			plr:Kick('suspicious purchase event call, possibly using cheats')
		end
		
		value.Value -= cost
		
		local module = require(noobsgiver[sph])
		local noob = module.noobgiver()
		print(noob.Name)
	end
end)

Purchase module:

local module = {}

local noobs = game:GetService('ServerScriptService').noobs.classic
local epic = noobs.Epic
local uncommon = noobs.Uncommon
local common = noobs.Common

module.noobs = {
	['Epic'] = {
		epic.CubicNoob;
	};
	['Uncommon'] = {
		uncommon.NegatiGhostyNoob;
		uncommon.GhostyNoob;
	};
	['Common'] = {
		common.NegatiNoob;
		common.ClassicNoob;
	}
}

module.rarity = {
	['Epic'] = 0.5;
	['Uncommon'] = 14.5;
	['Common'] = 85;
}

module.noobgiver = function()
	local rand = math.random(1,100) -- generate number of chance for each rarity (ex.: if rand = 0.5 then system will give random epic pet)
	
	local cout = 0
	
	for r, w in pairs(module.rarity) do -- r = rarity, w = for counter
		cout = cout + w
		if rand <= cout then
			local rTab = module.noobs[r]
			local noob = rTab[math.random(1,#rTab)]
			
			return noob
		end
	end	
end

return module

I dont think there was a mistake in the scripts, please someone respond :slight_smile:

P.S: by the way, dont ask me why the time is 09:09 AM, and im making this topic at 18:32 PM, its just because my pc sometimes have errors with the time

So in 1 purchase there are 3 prints, right?

Try using a debounce? Not sure if it would really help though…

Yes, there is 3 prints at the second purchase

already tried, but it didnt work

there are 3 modules inside noobsgiver? each one being the third script?

Is this event being called? Who is calling and how often?
The problem is that if this event is being called more than once, it will load the same functions, and it will repeat and duplicate.

Example:

When the event called once, it will execute this only once:

event.OnClientEvent:Connect(function(callType)
       yes.MouseButton1Click:Connect(function()
          print("test") --test will be printed only once
       end)
end)

Now if the event is called 3 times, it will execute 3 times the same function connected in the MouseButton1Click event

event.OnClientEvent:Connect(function(callType)  -- they called me 3 times :O
       yes.MouseButton1Click:Connect(function()
          print("test") --3x test in output
       end)
end)

You are right, didn’t notice that. You should disconnect the yes event once the purchase has been completed

thanks, but, how to disconnect the events?

local event = yes.MouseButton1Click:Connect(function()
				event2:FireServer({pt = 'Spheres',sphere='classic',lvl=ct.LvlReq,cost=ct.Cost})
				script.Parent.Visible = false
			end)
event:Disconnect()

do event:Disconnect() when the purchase has been finished

1 Like

Make variables for function connections:

local YesButtonConnection -- Put above the functions

Put in the code the value of the variable the function

YesButtonConnection  = yes.MouseButton1Click:Connect(function() --The variable will receive a connection like the return
        --CODE
         YesButtonConnection:Disconnect() -- the Disconnect() method will disconnect the function with the event and it will stop existing
end)
1 Like

ok, i will try to disconnect the events, thanks

Just to be sure, put this after:

event.OnServerEvent:Connect(function(plr,info)
    if YesButtonConnection or NoButtonConnection then --if the connection already exists, it will disconnect
           YesButtonConnection:Disconnect()
           NoButtonConnection:Disconnect()
    end 
    --CODE
end)
1 Like

thanks, its working perfectly now

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.