Best Way to Script Combat?

In the past, I made a combat script. The problem with the combat script was that when I fired the remote event, I passed a hit from a Touched Event which was bad because the client could just put anything in there and make a loop-kill exploit perhaps. When you press Q, there is a count variable, canPunch variable, and punching variable. Count increments by 1 each time after one of the combat animations play from the client so it can play the next one because there’s 5 in total. When I fired the event, the server code would just check if there was a hit, hit.Parent, and if the hit.Parent had a humanoid and damage accordingly. My old script worked but it wasn’t secure against exploiters.

I decided to remake the script and put mostly everything on the server, even the animation playing. On the client, when you press Q, it fires the combat event. The problem with this one is that when anyone in the game presses Q, it updates the canPunch, punching, and count variable for everyone in the server. Any ideas on a different approach to code my combat?

(Code removed due to question being answered)


I would make an array containing all the players in the server and having their respected variables assigned to them. Since OnServerEvent gives out the client’s details you can always lookup that player in the array. Like so:

local playerArray = {
	WatermelonArray = {Count = 0;}; -- Example how data would be structred when you add a player to it
	local playerData = playerArray[player.Name] -- Searches for that player in the array
	local count = playerData.Count -- You can then call out their own respected values per player

The idea is that when a player joins or leaves, you add or remove that new player to that array:

Players.PlayerAdded:Connect(function(plr) -- Gets a new player
	playerArray[plr.Name] = { -- Assigns a new array to the playerArray
		Count = 0; -- All the data you want is stored here

Players.PlayerRemoving:Connect(function(plr) -- Gets a player leaving
	playerArray[plr.Name] = nil -- Remove the data

This is only just a rough idea on how I would approach this.


Combat System Approach

Alright, first things first let’s go over how FE works and how we can come up with the best way to use this to account for latency and remote lag. Firstly, FE latency is around 0 to .3 seconds, not always, but mostly. Items made on the server will automatically replicate to all clients, and items made on the client will not replicate. (Also, the client is much better for handling things like Tween and CFrame, so if possible, be sure to use the client to move things.)
The server and client communicate with RemoteEvents and RemoteFunctions.

There are multiple approaches to do a combat system, and the one stated below is a rough sketch of how I would do it.

My approach
When punch is requested, play the animations and sounds on the client (we do this so the player can experience no delay in seeing their punch, so they feel like there is no lag when in reality there always is). Once this is done, we fire a remote like so:

We fire to let the server know we would like to punch something, but do not put any parameters in this!! By putting a player name or anything else you are creating a security risk, the client should not have any power. No power to the client!! The server will manage all the data.

Once the server receives this, the server will replicate the sounds and effects to the other clients by firing a remote to them.
The server can now manage the main interaction. From here, the server can calculate the closest player using magnitude, or preferably, use a .Touched event and deal the damage toward the other player if conditions meet up.

Of course, do not forget to add a cool down on the server so the exploiters cannot spam fire the punch event. And let’s add a cool down to the client as well so they cannot spam the client animations when pressing the punch button.

You can add a “-10” HP GUI above their hed when they are attacked as well if needed.

This is fully customizable and can be done in many more ways than 1. The solution I presented should be enough to suffice for multiple factors, but may not be exactly what you’re looking for. Be sure to play around with the code and conduct multiple tests.

I hope this helps and best of luck!


Both @WatermelonArray and @Beartikal together put a very good solution for this and I suggest you to use both. Array for players, animation playing from client, no power to client while firing events.

What my additional suggest is on Beartikal’s system. You should put a time limit for client which is equal to or a few lesser than server’s.


Ah yes, I forgot about that. Thanks for adding on!