Make other players get dmged 15 but not yourself

i want everyone health to be decerased by 15 but not the local player how do i do that?
for e.g
everyone total health is 100
when u press e
their health goes to 85
but your health stays the same

1 Like

Detect person who clicked e and pass that name into the Name Variable

Name = "Your Name"
for _,Player in pairs(game:GetService('Players'):GetPlayers()) do
	if Player.Name ~= Name then
		local Character = Player.CharacterAdded:Wait()
		local Humanoid = Character:WaitForChild('Humanoid')
		Humanoid:TakeDamage(5)
	end
end
2 Likes

but i want the others to take the dmg not me

1 Like

Yes, it will ignore you as long as whoever clicked e is assigned to the Name variable

1 Like
--LOCAL

local replicated = game:GetService("ReplicatedStorage")
local remote = replicated:WaitForChild("RemoteEvent")
local userInput = game:GetService("UserInputService")

userInput.InputBegan:Connect(function(input, processed)
	if processed then return end
	
	if input.KeyCode == Enum.KeyCode.E then
		remote:FireServer()
	end
end)
--SERVER

local players = game:GetService("Players")
local replicated = game:GetService("ReplicatedStorage")
local remote = replicated.RemoteEvent
local damage = 15

remote.OnServerEvent:Connect(function(player)
	for _, otherPlayer in ipairs(players:GetPlayers()) do
		if otherPlayer ~= player then
			local otherCharacter = otherPlayer.Character or otherPlayer.CharacterAdded:Wait()
			local otherHumanoid = otherPlayer:FindFirstChildOfClass("Humanoid")
			if otherHumanoid then
				otherHumanoid:TakeDamage(damage)
			end
		end
	end
end)

You’ll need a RemoteEvent instance placed inside the ReplicatedStorage folder.

1 Like

You should use a RemoteEvent and the event UserInputService.InputBegan to detect when a player presses the key: E

LocalScript:

local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local DamagePlayers = ReplicatedStorage:WaitForChild("DamagePlayers")

UserInputService.InputBegan:Connect(function(input, GPE)
	if input.KeyCode == Enum.KeyCode.E then
		DamagePlayers:FireServer()
	end
end)

Server script:

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

ReplicatedStorage.DamagePlayers.OnServerEvent:Connect(function(playerWhoFired)
	for i, player in pairs(Players:GetPlayers()) do
		if player ~= playerWhoFired then
			if player.Character then
				local Humanoid = player.Character:FindFirstChild("Humanoid")
				if Humanoid then
					Humanoid:TakeDamage(15)
				end
			end
		end
	end
end)

Edit: I didn’t notice that @Forummer replied with a solution similar to mine. I apologize.

1 Like