Best way to remote damage?

The title didn’t make any sense but basically

I was reading a post to create a fps framework where he set “damage” as a parameter for FireServer and then said not to do it and he had done it for beginners only

And so someone asked why we shouldn’t do it and he said that hackers could easily change and do infinite damage to everyone

So I want to know what is the best way to doing damage in which hackers have minimal access to do infinite damage

The post

1 Like

when he fired to the server he also sent a number and im guessing thats the amount of damage that should be dealt. I recommend firing it to the server but removing the number from the parameter and handling the damage on the server rather than sending the damage to the server.

The best way to do this is to damage on the server directly, the client would just tell the server to do damage. It would looks something like this

-- Client
game.ReplicatedStorage.Damage:FireServer(Someone)
-- Server
local DamageInfo = {
   HandGun = 10
   Rifle = 40
}

local PlayerGuns = {}

game.ReplicatedStorage.Damage.OnServerEvent:Connect(function(Host, Victim)
   -- Assuming you already know what gun the player has
   local Damage = DamageInfo[PlayerGuns[Host.UserId]]
   Victim.Humanoid:TakeDamage(Damage)
end)

This is the basic idea on how it goes, theres more you can add such as checking whether the damage request is consistent with the bullet replication’s request.

Hello blazepowerroblox!

The reason you should not allow the client to decide what damage is to be dealt is because exploiters will tamper with this and increase their damages to irregular amounts.

The solution?

Whenever the client fires what part they hit (unless you are raycasting on the server, not recommended for FPS games where hit registration is required to be precise), you can verify that X part is a part of a character, X part could have been hit by Y client, Y client had enough ammo to shoot X part, and keep a table of damages per weapon on the server.

local damages = setmetatable({
	["M4A1"] = setmetatable({
		["Head"] = {100,110},
		["Torso"] = {50,60},
		["Left Arm"] = {30,40},
		["Right Arm"] = {30,40},
		["Left Leg"] = {30,40},
		["Right Leg"] = {30,40}
	},{
		__index = function(data,indexed)
			return {30,40}
		end,
	})
},{
	__index = function(data,indexed) --we use metatables so that we can prevent errors from occurring if X gun does not have damage values set
		return setmetatable({
			["Head"] = {100,110},
			["Torso"] = {50,60},
			["Left Arm"] = {30,40},
			["Right Arm"] = {30,40},
			["Left Leg"] = {30,40},
			["Right Leg"] = {30,40}
		},{
			__index = function(data,indexed)
				return {30,40}
			end,
		})
	end,
})
--
local function processhitregister(player,hitpart) --will return the player and the amount they should be damaged for
	if player.Character then
		local tool = player.Character:FindFirstChildOfClass("Tool")
		if tool then
			if tool:FindFirstChild("CurrentAmmo") then --this is an example of an ammo check
				if tool.CurrentAmmo.Value > 0 then --this is an example of an ammo check
					local player2 = game.Players:GetPlayerFromCharacter(hitpart.Parent)
					if player2 then
						local random = Random.new(os.time())
						local damagetable = damages[tool.Name][hitpart.Name]
						return player2,random:NextInteger(damagetable[1],damagetable[2])
					end
				end
			end
		end
	end
end
1 Like

You just told me how headshots work :grin:

Actually I wanted to ask this because I am making a brick throwing kind of thing and wanted to know if there was a good way to do it. Will this work tho?

Hello blazepowerroblox!

It appears like you have marked this topic as ‘solved.’ If you would like further assistance, please either remove the solution marking or create a new topic. Or, alternatively, if the solution you have marked is not working for you as you hoped, unmark it as a solution and reply for more assistance.

I have removed it and I as I asked will this code work if I am making a brick throwing thingy

Hello blazepowerroblox!

My code will work with anything as long as you call the function processhitregister with parameters player,hitpart, player being the player who has fired their weapon, thrown the brick, etc and hitpart being the part hit by the bullet or brick.

1 Like