How Can I Prevent This?

Hello, My Name Is Nehoray

I Have NPC In my game and this is what’s i’m trying to prevent

Exploiter Could Just Run this Code and all of my NPC’s are dead:


while wait(0.2) do
	for i, v in pairs(game.Workspace:GetChildren()) do
		if v:IsA("Model") then
			if v.Name == "NPC" then
				local Pos = v.Torso.Position
				game.ReplicatedStorage.Fire:FireServer(Pos)
			end
		end
	end
end

Video:
https://gyazo.com/53ee6700ba1969d13327c21ac26c56f6

How should i prevent this?

Thanks.

Can you post your server script so we can better help you?

1 Like

Server:

game.ReplicatedStorage.Fire.OnServerEvent:Connect(function(player, MousePos)
	if MousePos ~= nil then
		
		local RaycastParem = RaycastParams.new()
		RaycastParem.FilterDescendantsInstances = {player.Character}
		RaycastParem.FilterType = Enum.RaycastFilterType.Blacklist
		
		local raycastReuslt = workspace:Raycast(script.Parent.Handle.Position,(MousePos - script.Parent.Handle.Position)*250,RaycastParem)
		
		if raycastReuslt then
			local hitPart = raycastReuslt.Instance
			local model = hitPart:FindFirstAncestorOfClass("Model")
			if model then
				if model:FindFirstChild("Humanoid") then
					model.Humanoid.Health -= 45
					if model.Humanoid.Health < 1 then
						model:Destroy()
					end
				end
			end
		end
	end
end) 

Are players supposed to hold out a tool? If so, you could check if the player is holding the tool.

You can also implement a cooldown so players can not fire the remote and kill all the NPCs in a matter of seconds.

1 Like

Players not supposed to hold the tool

just click

still they can just go on every NPC and kill him
but i will add debounce

Thanks

You can also utilize Mouse.Target to detect what the player is clicking on.

I would recommend implementing a distance check so a player couldn’t just click across the map to click your NPC and deal damage.

local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()

local NPC = game.Workspace.NPC

local nHPart = NPC.HumanoidRootPart

local maxDistance = 20

if player:DistanceFromCharacter(nHPart.Position) <= maxDistance then
 -- code
end

If I misunderstood the purpose of your script, let me know.

1 Like