Any better way to do this debounce?

Is there a better way of doing a debounce that doesnt stop lining up from client to server

Currently had to shorten the time on the server, but not entirely sure if this is true but people with higher pings will see the bullets unallign with the damage

Script

Client

local Tool = script.Parent
local player = game.Players.LocalPlayer
local UIS = game:GetService("UserInputService")
local Event = Tool:WaitForChild("MainEvent")
local RepEvent = game.ReplicatedStorage:WaitForChild("RepEvent")
local Bullet = workspace.Ignored.ClientBullet
local Debris = game:GetService("Debris")
local IsUsing = false
local debounce = false
local IsHeld = false

-- For Easier Changing --
local BulletSize = 0.2
local BulletColor = Color3.new(1,1,0)
local RayLength = 300
-------------------------

local Params = RaycastParams.new()
Params.FilterDescendantsInstances = {}
Params.FilterType = Enum.RaycastFilterType.Blacklist


local function OnEquipped()
	IsHeld = true
	local C = {}
	local Holder = Tool.Parent
	table.insert(C, Holder)
	for i, v in ipairs(workspace.Ignored:GetChildren()) do
		table.insert(C, v)
	end
	Params.FilterDescendantsInstances = C
end

local function OnUnequip()
	IsHeld = false
	IsUsing = false
end

local function BulletGen(ray)
	local Rv = ray.Direction * RayLength
	local Raycast = workspace:Raycast(ray.Origin, Rv, Params)

	local HitPos = if Raycast then Raycast.Position else ray.Origin + Rv

	local ShootFrom = Tool["First Ak"]["Meshes/Ak_Metal"].Attachment.WorldPosition

	local AimAt = HitPos - ShootFrom
	local HitDetection = workspace:Raycast(ShootFrom, AimAt * 2, Params)

	local ThingHit = if HitDetection then HitDetection.Instance else nil

	local PosHit = if HitDetection then HitDetection.Position else HitPos
	
	local D = Bullet:Clone()
	D.Color = BulletColor
	D.Size = Vector3.new(BulletSize, BulletSize, (ShootFrom - PosHit).Magnitude)
	D.CFrame = CFrame.lookAt(ShootFrom:Lerp(PosHit, .5), PosHit)
	D.Parent = workspace.Ignored
	Debris:AddItem(D, .05)
end

local function ClientBullet(From, To)
	local ReplicatedBullet = Bullet:Clone()
	ReplicatedBullet.Color = BulletColor
	ReplicatedBullet.Size = Vector3.new(BulletSize, BulletSize, (From - To).Magnitude)
	ReplicatedBullet.CFrame = CFrame.lookAt(From:Lerp(To, .5), To)
	ReplicatedBullet.Parent = workspace.Ignored
	Debris:AddItem(ReplicatedBullet, .05)
end

Tool.Activated:Connect(function()
	IsUsing = true
end)

Tool.Deactivated:Connect(function()
	IsUsing = false
end)

game:GetService("RunService").Stepped:Connect(function()
	if IsUsing == true then	
		if debounce == true then return end
		if IsHeld == false then return end
		debounce = true
		local mouse = UIS:GetMouseLocation()
		local ray = workspace.CurrentCamera:ViewportPointToRay(mouse.X, mouse.Y)
		Event:FireServer(ray)
		BulletGen(ray)
		task.wait(.1)
		debounce = false
	end
end)


Tool.Equipped:Connect(OnEquipped)
Tool.Unequipped:Connect(OnUnequip)
RepEvent.OnClientEvent:Connect(ClientBullet)

Server

local Tool = script.Parent
local Event = Tool.MainEvent
local RepEvent = game.ReplicatedStorage:WaitForChild("RepEvent")
local Params = RaycastParams.new()
local IsHeld = false
local debounce = false
Params.FilterDescendantsInstances = {}
Params.FilterType = Enum.RaycastFilterType.Blacklist

local function OnEquipped()
	IsHeld = true
	local C = {}
	local Holder = Tool.Parent
	table.insert(C, Holder)
	for i, v in ipairs(workspace.Ignored:GetChildren()) do
		table.insert(C, v)
	end
	Params.FilterDescendantsInstances = C
end

local function OnUnequip()
	IsHeld = false
	-- More here later
end

local function Shoot(Player, ray)
	if debounce == true then return end
	if IsHeld == false then return end
	debounce = true
	local Rv = ray.Direction * 300
	local Raycast = workspace:Raycast(ray.Origin, Rv, Params)
	
	local HitPos = if Raycast then Raycast.Position else ray.Origin + Rv
	
	local ShootFrom = Tool["First Ak"]["Meshes/Ak_Metal"].Attachment.WorldPosition
	
	local AimAt = HitPos - ShootFrom
	local HitDetection = workspace:Raycast(ShootFrom, AimAt * 2, Params)
	
	local ThingHit = if HitDetection then HitDetection.Instance else nil
	
	local PosHit = if HitDetection then HitDetection.Position else HitPos
	
	if ThingHit then
		local L_ = ThingHit:FindFirstAncestorOfClass("Model")
		if L_ then
			local Humanoid = L_:FindFirstChildWhichIsA("Humanoid")
			if Humanoid and Humanoid.Health > 0 then

				if Humanoid:GetAttribute(Player.name) then
					local Attribute = Humanoid:GetAttribute(Player.name)
		
					Humanoid:SetAttribute(Player.name, Attribute + math.clamp(12, 0, Humanoid.Health))
				else
					Humanoid:SetAttribute(Player.name, 12)
				end

				Humanoid:TakeDamage(12)
			end
		end
	end

	for i, v in ipairs(game.Players:GetPlayers()) do
		if v ~= Player and v then
			RepEvent:FireClient(v, ShootFrom, PosHit)
		end
	end
	task.wait(.04)
	debounce = false
end



Event.OnServerEvent:Connect(Shoot)
Tool.Equipped:Connect(OnEquipped)
Tool.Unequipped:Connect(OnUnequip)
1 Like