I need help on making a click to damage script

I am making a game that uses custom click to move for player movement.
So i need a good way to do a click to damage script
(Click an NPC and your character will walk to it and damage it)

I have tried Mouse.Target.Parent to get the NPC then check if it has a Humanoid:

if Mouse.Target.Parent:FindFirstChild("Humanoid") then
--damage code
end

Then i check the magnitude between the player and the npc to make it only hit when the player is in range.

But then i run into the problem of this script being on the client not the server.
Got any ideas?

3 Likes

Well first of, you can check the magnitude on the client and if its in the appropriate range, Fire a remote event. But you’ll have to run checks on the server, Checking the distance between the NPC and the player incase the client sent wrong info, That will insure that the NPC is in range, If all those meet you can finally deal the damage.

2 Likes

If you want the damage to register on the server, you’ll need to send the damage, player, and target player with a remote event, and then deal the actual damage on the server.

In order to communicate with the Server you’ll need to use a RemoteEvent and fire it from the client. If you’re new to this concept, reading this text will explain to you in detail how it works, along with code samples for you to use.

In order to prevent abuse by exploiters firing your event to do damage, there are a few things you should keep in mind.

  • Don’t let the client say how much damage should be done
    If you let this happen, I could fire the remote with 99999 damage in a loop and kill everyone in the server repeatedly. Instead have a present amount determined by the server.
  • Make sure you check magnitude on the server.
    If you do so on the client, the server assumes the client is right. That way, someone who is not close enough could still fire the event and the server will think it’s perfectly valid.

In general, your code should look something like this:

local LocalPlayer = game.Players.LocalPlayer
local Remote = LOCATION_NAME.RemoteEvent

-- LOCALSCRIPT

-- After we have detected a click and determined that it is an NPC, we do the following:

Remote:FireServer(NPCModel)

-- SERVER

Remote.OnServerEvent:Connect(function(Player, NPCModel)
    if Player:DistanceFromCharacter(NPCModel.Torso.Position) <= MINIMUM_DISTANCE then
          NPCModel.Humanoid:TakeDamage(DAMAGE_AMOUNT)
    end
end)


This code should be used as a learning reference and not copy pasted into your game.

As you can see, the only thing the client is sending is the NPC Character Model, and nothing else. This is because we are not trusting the client, as this could lead to an exploiter problem.

The damage is determined on the server too, so people can’t tell the server how much damage to do. Likewise with the magnitude, we don’t let anyone tell us how far they are, as this too can be falsified.

Hope this helped.

8 Likes

Yeah i made the scripts already
but i did not know about people being able to hack the client to do more damage
i will make those changes now.