How can I make this un spammable?

  1. What do you want to achieve? A script that when you click Q and then use your mouse to click on a player or a burnable object it will fire to the server and then handle all the effects in the server

  2. What is the issue? the script I have is spammable. They can click Q a bunch of times and the click on a player or object and it’ll fire the remote more than once which is what I want to get rid of

  3. What solutions have you tried so far? I have tried using debounces, disconnecting the connection as soon as possible but nothing has worked

here’s the whole script

local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local WorkspaceService = game:GetService("Workspace")


local player = game.Players.LocalPlayer
local character = player.Character
local currentCamera = workspace.CurrentCamera
local mouse = player:GetMouse()

local Event = ReplicatedStorage.WitchRemotes:FindFirstChild("Pyrokinesis")

local debounce = false
local connection

local function getMouseHit()
	local mouseLocation = UserInputService:GetMouseLocation()
	local viewportPointRay = currentCamera:ViewportPointToRay(mouseLocation.X, mouseLocation.Y)
	local extendedRay = Ray.new(viewportPointRay.Origin, viewportPointRay.Direction * 1000)
	return WorkspaceService:FindPartOnRay(extendedRay)
end

UserInputService.InputBegan:Connect(function(input, processed)
	if input.KeyCode == Enum.KeyCode.Q and not debounce then
		connection = mouse.Button1Down:Connect(function()
			debounce = true
			connection:Disconnect()
			local hit, position, normal = getMouseHit()
			if hit and hit.Parent:FindFirstChild("Humanoid") then
				if hit.Parent == character then return end
				local distance = math.abs(hit.Parent.HumanoidRootPart.Position.Magnitude - character.HumanoidRootPart.Position.Magnitude)
				if distance <= 10 then
					debounce = true

					print("Player")
					Event:FireServer("Player", hit.Parent)

					task.wait(1)
					debounce = false
				end
			elseif hit and hit:FindFirstChild("CanBurn") then
					local distance = math.abs(hit.Position.Magnitude - character.HumanoidRootPart.Position.Magnitude)
					if distance <= 10 then
						debounce = true

						print("Object")
						Event:FireServer("Object", hit)

						task.wait(1)
						debounce = false
					end
				end
			end
		end)
	elseif input.KeyCode == Enum.KeyCode.Q and debounce == true then
		return
	end
end)

all help is appreciated

for the spamming Q, I’d set it up like this instead

qPressed = false
debounce = false

on input began
    if (keyCode is Q) and (not debounce)
      qPressed = true
   elseif (inputType is MouseButton1) and (qPressed) and (not debounce)
      debounce = true
      qPressed = false

      handle the click
      wait(1)
      debounce = false
   end
end

on input ended
   if keyCode is Q
      qPressed = false
   end
end

also, I’d remove the debounce in the local script so they can spam in the local script, but then handle the debounce on the server to stop the users from triggering things too often

idk if this is the same as how your script should work, so you might need to take out the on input ended

1 Like