Help with spamming parts

In my game, you can place blocks freely just by pressing “j”. Does anyone know how to change this script to limit it to only 1 cube per second?

RemoteEvent name: BIG CUBE

LocalScript in StarterPlayerScripts:

local UIS = game:GetService("UserInputService")
local Event = game.ReplicatedStorage["BIG CUBE"]

UIS.InputBegan:Connect(function(key,typing)
	if typing then return end
	if key.KeyCode == Enum.KeyCode.J then
		Event:FireServer()
	end
end)

Script in ServerScriptService:

local Event = game.ReplicatedStorage["BIG CUBE"]

Event.OnServerEvent:Connect(function(Player)
	local Part = Instance.new("Part")
	Part.Size = Vector3.new(3,3,3)
	Part.Parent = game.Workspace
	Part.BrickColor = BrickColor.new("Ghost grey")
	Part.Material = Enum.Material.SmoothPlastic
	Part.CFrame = CFrame.new(Player.Character.HumanoidRootPart.Position + (Player.Character.HumanoidRootPart.CFrame.LookVector * 10))
end)

Thanks in advance for any help!

Add a debounce

I’m a little new to scripting so I don’t know how to add that in. Like I don’t know where to put it and what to replace.

Example:

local debounce = true

— your stuff—

If debounce == true then
debounce = false

—your stuff—

wait(1)
debounce = true
end

So would I put that I put that in my serverscript and just put what I have inside where you put “your stuff”?

Something like this, separate cooldown for every player

local Event = game.ReplicatedStorage["BIG CUBE"]

local CoolDown = 1
local Debounces = {}
Event.OnServerEvent:Connect(function(Player)
	-- separate cooldown for each player
	local Last = Debounces[Player.UserId]
	-- if last time doesn't exist then set it
	if not Last then
		Debounces[Player.UserId] = 0
		Last = 0
	-- if difference between current time and last time is less than cooldown then do nothing
	elseif time() - Last < CoolDown then
		return
	end
	
	-- set last time to current time
	Debounces[Player.UserId] = time()
	
	local Part = Instance.new("Part")
	Part.Size = Vector3.new(3,3,3)
	Part.Parent = game.Workspace
	Part.BrickColor = BrickColor.new("Ghost grey")
	Part.Material = Enum.Material.SmoothPlastic
	Part.CFrame = CFrame.new(Player.Character.HumanoidRootPart.Position + (Player.Character.HumanoidRootPart.CFrame.LookVector * 10))
end)
3 Likes

Thank you!! Very much appreciated!