Script help with a drink gui!

Hello there! I got a script which opens a gui which did not had a debounce.
The debounce works now tho.

Unfornatuely the important part of the script is not working correctly now.
Sometimes it works for the first time and then it completely breaks and sometimes it does not works at all!



image

I couldn’t find any solutions on the devforum!

The script used to work when the script was in Starter Gui. Now it is in a gui giver script with a ontouch function.
It would be great if someone could explain me why it is not working and help me!
Thanks for reading! :grinning:

Script in the drink buttons:

local p = game.Players.LocalPlayer

script.Parent.MouseButton1Click:Connect(function()
	wait(3.2)
	local tool = script.Sevenup:Clone() 
	tool.Parent = p.Backpack
	p.Backpack.Cup:destroy()
end)

script.Parent.MouseButton1Click:Connect(function()

	wait(0.1)
	script.Parent.Parent.Visible = false	
	script.Parent.Parent.Parent.PreparingGui.Timer.Text = 3
	wait(1)
	script.Parent.Parent.Parent.PreparingGui.Timer.Text = 2
	wait(1)
	script.Parent.Parent.Parent.PreparingGui.Timer.Text = 1
	wait(1)
	script.Parent.Parent.Parent.PreparingGui.Timer.Text = 0
	wait(0.3)
	script.Parent.Parent.Parent.PreparingGui.Visible = false
	script.Parent.Parent.Parent:destroy()
end)

Script which opens the gui:


local buttonPressed = false
local gui = script.DrinkGui

function onTouched(p)
	if not buttonPressed then
		buttonPressed = true
		if p and p.Parent and game.Players:FindFirstChild(p.Parent.Name) ~= nil then
			local player = game.Players:FindFirstChild(p.Parent.Name)
			if player.PlayerGui:FindFirstChild("ShopGui") == nil then
				local newgui = gui:Clone()
				newgui.Parent = player.PlayerGui
				wait(2)
				buttonPressed = false
			end
		end
	end
end

script.Parent.Touched:Connect(onTouched)

ROBLOX does not guarantee a valid timestamp of which objects are replicated from server to client, or vice versa. Therefore, I personally would recall an instance of the script by waiting for the tool to initially load in:

script:WaitForChild('Sevenup'):Clone()

Alright, thank you! I am gonna try that.

I still need a solution as I am releasing the update today! Please help me.