Hitbox and part delay

So I’ve created a basic skill with a hitbox, but it seems like the part is delayed from the hitbox. Should I try using coroutine for this? Any advice or tips would be appreciated
Heres a video:

The red is the hitbox and the gray is the part.
local script

local replicatedStorage = game:GetService("ReplicatedStorage")
local tweenService = game:GetService("TweenService")
local debris = game:GetService("Debris")
local runService = game:GetService("RunService")

local hitboxModule = require(replicatedStorage:WaitForChild("HitboxModule"))

local remoteEvent = replicatedStorage:WaitForChild("RemoteEvent")
local remoteEvent1 = replicatedStorage:WaitForChild("RemoteEvent1")

local part = replicatedStorage:WaitForChild("Part")
local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()

local tool = script.Parent


tool.Activated:Connect(function()
	local mouseHit = mouse.Hit
	local hrp = player.Character.HumanoidRootPart
	local clonePart = part:Clone()
	clonePart.CFrame = hrp.CFrame
	clonePart.Transparency = 1
	debris:AddItem(clonePart, 10)
	
	remoteEvent1:FireServer(mouseHit)

	local params = OverlapParams.new()
	params.FilterDescendantsInstances = {player.Character}
	params.FilterType = Enum.RaycastFilterType.Blacklist

	local hitbox = hitboxModule.createBox()
	hitbox.Shape = Enum.PartType.Ball
	hitbox.Size = 4
	hitbox.OverlapParams = params
	hitbox.CFrame = clonePart

	local setAngle = clonePart.CFrame.Rotation

	local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.In)
	local goal = {CFrame = CFrame.new(mouseHit.Position) * setAngle}
	local tween = tweenService:Create(clonePart, tweenInfo, goal)
	tween:Play()
	hitbox:Play()

	hitbox.Touched:Connect(function(hit, hum)
		if hit and hum then
			remoteEvent:FireServer(hit, hum, mouseHit)
		end
	end)

	clonePart.Parent = workspace
end)

remoteEvent1.OnClientEvent:Connect(function(player, mouseHit)
	local hrp = player.Character.HumanoidRootPart
	local clonePart = part:Clone()
	clonePart.CFrame = hrp.CFrame
	debris:AddItem(clonePart, 10)

	local setAngle = clonePart.CFrame.Rotation

	local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.In)
	local goal = {CFrame = CFrame.new(mouseHit.Position) * setAngle}
	local tween = tweenService:Create(clonePart, tweenInfo, goal)
	tween:Play()
	clonePart.Parent = workspace
end)

server script

local replicatedStorage = game:GetService("ReplicatedStorage")

local remoteEvent = replicatedStorage:WaitForChild("RemoteEvent")
local remoteEvent1 = replicatedStorage:WaitForChild("RemoteEvent1")

remoteEvent.OnServerEvent:Connect(function(player, hit, hum)
	hum:TakeDamage(10)
end)

remoteEvent1.OnServerEvent:Connect(function(player, mouseHit)
	remoteEvent1:FireAllClients(player, mouseHit)
end)

By the looks of your code, the reason the part is lagging behind the hitbox is because it has to go through 2 remote events before anyone sees the part.

For example: If your ping is 50, and you click, this is what happens:

0ms since start: Hitbox created and tweened

50ms since start: Server recieves function to create part, tells all clients
(Hitbox has been moving for 0.05 seconds)

100ms since start: Clients recieve event to create part and tweens it.
(Hitbox has been moving for 0.1 seconds, giving you a 0.1 second delay)

A way you should fix this, is to first, create the hitbox on the server.
If your client is the one creating and telling the server when the hitbox gets touched, then exploiters can easily abuse that system, by just telling the server that they hit someone without even having to click.

You have to remember, anything that isnt in ServerScriptService or ServerStorage, exploiters can access and use.
Anyone who owns SynapseX or krnls (should or might) be able to run this code and kill everyone in the server.

game:GetService("RunService").RenderStepped:Connect(function()
	for i, v in pairs(game.Players:GetPlayers()) do
		if v.Character and v.Character:FindFirstChild("Humanoid") then
			remoteEvent:FireServer(v.Character.Head, v.Character:FindFirstChild("Humanoid"), game.Players.LocalPlayer:GetMouse().Hit)
		end
	end
end

Second: Create the part on the client first, then replicate it to other clients.
This will give the smooth appearance to everyone, and make the hitbox mostly line up

-- This is the server script that tells the other clients to create the part

-- It will also avoid telling the player that fired the event to create the part
-- as they should have already made it on their end BEFORE firing this event
remoteEvent1.OnServerEvent:Connect(function(player, mouseHit)
	for i, v in pairs(game.Players:GetPlayers()) do
		if v ~= player then
			remoteEvent1:FireClient(v, player, mouseHit)
		end
	end
end)
1 Like

Do you think it would be secure if I created a module script inside of Replicated Storage with the hitbox and part information in it then call it from the local script? The scripts on here is more or less for reading convenience.

No, replicated storage can be accessed by exploiters.

1 Like
local replicatedStorage = game:GetService("ReplicatedStorage")
local debris = game:GetService("Debris")

local remoteEvent = replicatedStorage:WaitForChild("RemoteEvent")
local part = replicatedStorage:WaitForChild("Part")

local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()
local tool = script.Parent

local debounce = false

tool.Activated:Connect(function()
	
	--if debounce == false then
		--debounce = true
		local hrp = player.Character.HumanoidRootPart
		local prevPos = hrp.Position
		
		local clonePart = part:Clone()
		clonePart.CFrame = hrp.CFrame
		debris:AddItem(clonePart, 1)
	
		hrp.CFrame = hrp.CFrame * CFrame.new(0, 0, -15)

		remoteEvent:FireServer(prevPos, clonePart.CFrame)
		--wait(2)
		--debounce = false
	--end
		
end)

So this is on a local script, another very simple script. Which values can the exploiters change? Would they be able to change the -15 to whatever they want?

Technically yes, but you have to always think… How bad would an exploit like this affect the game?

I mean, you are changing a cframe on the client, so that probably isn’t replicating to the server anyway (Not sure if a player.Character’s cframe would replicate)

So would their changing the -15 really affect anything happening on the server or other clients? Probably not. Its all about minimizing disruption of your game play and preventing cheating, by thinking it through, as to what is getting to the server.

Also, because something like this is in a client script, but is not being sent to the server via an event, its less likely it would be exploited than if you are setting attributes, or sending data on events.

1 Like

That makes sense! Do you have any tutorials or videos on a debounce system? I know that the debounce I put in this script isnt the best and I’d like to learn how to make a decent one. Would debounce be cliet-sided?

Maybe I don’t quite get what your code is trying to do, but that is how I use a debounce system.
I mean, if you are just wanting to make sure someone is not spamming the Activate event on a Tool, then what you have should work.

The script clones a part where the humanoid is then teleports the player then fires an event to the server. On the server, a hitbox is created where the part is.

Would an exploiter be able to go in and change the wait() time to 1 second and fire this every second? It’s the cooldown for the teleport ability. If they were, I’d assume that from your previous comments, nothing would change on the server?

I think you should do all of this on the server.
Clone the parts (ball and hitbox) on the server.
Also do the teleport on the server.