Toggle cooldown NOT Working

I have a 3d gui that pops up and tweens it self into the shape its going to be in, that works fine.

My Problem is that it will toggle multiple times if i spam it when there should be a sure fire cooldown and shouldnt not work.

I need this to not be spammable and I cant find a solution(yes chat gpt was used)
Heres the code(not too pretty)

local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local Humanoid = char:FindFirstChildOfClass("Humanoid")
local CurrentCamera = workspace.CurrentCamera

local UIS = game:GetService("UserInputService")

local AnimTrack = Humanoid:LoadAnimation(game.ReplicatedStorage.Animations.LookAtMainGui)
AnimTrack.Priority = Enum.AnimationPriority.Action4

local GuiOpen = false

local endSize = Vector3.new(13, 11, 1)
local startSize = Vector3.new(1, 1, 1)
local curGuiObj

local beforeJump = Humanoid.JumpHeight
local beforeWalk = Humanoid.WalkSpeed

local beforeCamCframe



local function ToggleGui()
	GuiOpen = not GuiOpen
	if cd then return end
	if GuiOpen then
		beforeCamCframe = CurrentCamera.CFrame
		
		local newPart = game.ReplicatedStorage.Presets.PortfolioPart:Clone()
		curGuiObj = newPart
		newPart.CFrame = char.HumanoidRootPart.CFrame * CFrame.new(4,3.5,-8) * CFrame.fromEulerAngles(math.rad(5),math.rad(-10), 0)
		newPart.Size = startSize
		Humanoid.JumpHeight = 0
		Humanoid.WalkSpeed = 0
		
		local clonedSurfaceGui = game.ReplicatedStorage.Presets.PortGui:Clone()
		clonedSurfaceGui.Parent = newPart
		
		newPart.Parent = workspace
		game:GetService("TweenService"):Create(newPart, TweenInfo.new(.8,Enum.EasingStyle.Quart), {Size = endSize}):Play()
		AnimTrack:Play(.2, 10000, 1)
		task.wait(.5)
		AnimTrack:AdjustSpeed(0)
		CurrentCamera.CameraType = Enum.CameraType.Scriptable
		local newPoint = char:FindFirstChild("HumanoidRootPart").CFrame * CFrame.new(3,3,3)
		local newRotation = newPart.Position - newPoint.Position
		local newCFrameRotation = {
			["X"] = math.rad(0);
			["Y"] = math.rad(newRotation.Y);
			["Z"] = math.rad(0);
		}
		game:GetService("TweenService"):Create(CurrentCamera, TweenInfo.new(.8,Enum.EasingStyle.Quart), {CFrame = char:FindFirstChild("HumanoidRootPart").CFrame * CFrame.new(4,3,3) * CFrame.fromEulerAngles(newCFrameRotation["X"],newCFrameRotation["Y"],newCFrameRotation["Z"])}):Play()
	else

		local tween = game:GetService("TweenService"):Create(curGuiObj, TweenInfo.new(.8,Enum.EasingStyle.Quart), {Size = startSize}):Play()
		game:GetService("TweenService"):Create(CurrentCamera, TweenInfo.new(.8,Enum.EasingStyle.Quart), {CFrame = beforeCamCframe}):Play()
		AnimTrack:Play(.2, 10000, -1)
		task.wait(.5)
		curGuiObj:Destroy()
		curGuiObj = nil
		CurrentCamera.CameraType = Enum.CameraType.Custom
		task.wait(.3)
		Humanoid.JumpHeight = beforeJump
		Humanoid.WalkSpeed = beforeWalk
	end
	
end


local cd = false
UIS.InputBegan:Connect(function(inp,gpe)
	if gpe or cd == true then return end
	
	if inp.KeyCode == Enum.KeyCode.Home then
		ToggleGui()
		cd = true
		task.wait(3)
		cd = false
	end
end)
1 Like

first of all why are you using the home key as the input to toggle, is that intentional or just chatgpt?

thats intentional, Chatgpt didnt help. Ive learned to not use it as my main source of code. But I can spam the home button and it will break, i dont know how it does but it does break.

Problem was i put the cd = true after the function rather than before it. This is because functions branch off into the function and resume when its finished, in the time i was pressing the input and it was activating multiple times causing it to break.

ah alr, i forgot that functions yeilded for one another lol

1 Like

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