I do damage to all humanoids instead of one

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to make it when i press ‘E’ close to an enemy it damages them.
    i have the bilboardGui and everything set-up

  2. What is the issue? Include screenshots / videos if possible!
    i damage all the humanoids instead of the one i pressed ‘E’ on.

    . And one it doesn’t do anything.
    I know the problem is with this…

local Player = game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart")
local UIS = game:GetService("UserInputService")

local human = game.Workspace.Human.HumanoidRootPart -- this is the problem.

local event = game.ReplicatedStorage.RemoteEvent

	if keyCode.keyCode == Enum.KeyCode.E then
		if (human.Position - Player.Position).magnitude < 18 then

I just don’t know how to fix it.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have tried using a tool and send raycasts. That kinda worked but not what i wanted.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
I use a local script to fire an event to damage the humanoids. The normal script is placed in the NPCS.
And the localscript is placed in starterGUI.

local humanoid = script.Parent.Humanoid

local Event = game.ReplicatedStorage.RemoteEvent


This is my first topic so sorry if i did something wrong.
Thank you

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

You fire the remote when you are near one human, but you put the scripts in all of them so when it fires it run those scripts on both the humans and they both take damage.

but the event wouldn’t fire when i pressed ‘E’ on the left human.

You could check the distance in the script when the remote fires

You could check the distance between the player who pressed the “E” keycode and the other character’s humanoid. If the distance is less than by a number (say 5 magnitude), then it damages the nearest humanoid.

You could fire the event with different parameters for each Humanoid and combine with using magnitude.
(Also I think you should check the magnitude on the server)

Alright, so when you made the human variable there are 2 humanoids in workspace with the same name. You would need to change the name of the humanoid models

How can i check the magnitude on the server if i can’t access the localplayer on the server?

Fire the even even with
The player being the person pressing e
Hit being the humanoid

1 Like

Thank you

I will try that!
[30 characters]

1 Like

Identify the humanoid when you find out which one is it whenever you’re near them. Then, fire an event to damage it.

There’s tons of methods to do this, but I imagine that you’re already doing it since I’ve noticed that an “E” pops up when you’re close to one. You can include this in the same script that you handle those.

(assuming you use RenderStepped)

-- your function of finding which humanoid is the nearest
-- show E above character
-- E pressed then
local humanoid = closestHumanoidThatWasFound

in another script:

RemoteEvent.OnServerEvent:Connect(function(player, humanoid)
if (humanoidRootPart.Position - playerRootPart.Position).magnitude < 18 then -- ALWAYS make sanity checks via the server to prevent exploits firing events.

Something along the lines of that.

The ‘E’ popping up is just using a bilboardGui and maxDistance to 18. I didn’t use any script for it.
Im not using renderstepped but i’ll look into that.
Pretty sure it’s an easy fix.
Thank you for your time.

Ah, then I suggest you to look up on how to do an “E to interact” system. An example can be found here: https://youtu.be/LtQmj3YgLnE

I will do that.
I watched this video: https://www.youtube.com/watch?v=4SzR9yck32o

Still haven’t been able to find anything.
It looks so easy to fix.
I will find a solution even if it takes weeks

I have thought about making a table and by using a loop, putting all humanoids in that table.
then by doing table.sort, ill have the closest humanoid. But i haven’t find a way to implement that in my code.

local player = game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart")

function findClosestHuman()
	local tbl = {}
	for _,v in pairs(workspace.Humans:GetChildren()) do
		tbl[#tbl+1] = {v; (v.HumanoidRootPart.Position - player.Position).magnitude}
	table.sort(tbl, function(near, far)
		return near[2] < far[2]	
		local NearestHumanoid = tbl[1]

while wait(1) do

I finally got it. This is the answer i’ve been looking for.