How to decrease life to other players when I play an animation

¿Hello, does anyone know how to reduce the life of other players when I play an animation?

I have a local script where I reproduce the animation when pressing a key (Q) and it is connected with a remote event to a script (serverScript).

In the localscript I have everything programmed so that an animation is played by pressing the letter “Q”.

I would like that after doing that (playing the animation) he inflicts damage to any player who is close to him and who reaches him with the animation, how could I do this?

Please, ¿could someone help me with that? thanks :innocent:

**LocalScript:**
local player = game.Players.LocalPlayer
repeat wait() until player.Character.Humanoid
local humanoid = player.Character.Humanoid
local stopAnim = player.Character
local mouse = player:GetMouse()

local anim = Instance.new("Animation")
anim.AnimationId = "rbxassetid://6229874629"
	--"http://www.roblox.com/asset/?id=6211104667"		-- dance 507771019		--"rbxassetid://6211104667" -- 5422511206
		
UserInputService = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

UserInputService.InputBegan:Connect(function(input, gameProccesedEvent)
	if input.KeyCode == Enum.KeyCode.Q then
		print("Q pressed, playing the anim")
		local playAnim = humanoid:LoadAnimation(anim)
		playAnim:Play()
		script.Anim:FireServer()
	end
end)

and the server script:

script.Parent.Parent.Anim.OnServerEvent:Connect(function()
	print("Conected to server..")
end)
2 Likes

You’d need to look into Magnitude with a default range of how close they have to be. It’d look something like this:

(player.HumanoidRootPart.Position - model.PrimaryPart.Position).magnitude
1 Like

does it work to know the distance between two players too and any model? :open_mouth:

I only typed model.Primary part so it wouldn’t be confusing but I guess I overthought that lol.

No it doesn’t, all you’d need is the position of the person playing the animation and anyone else who is in game. A simple loop going through magnitudes then seeing if they’re close enough then applying damage will work.

1 Like

This script would damage any player who is within a specified distance of the attacker. Change your server script to:

script.Parent.Parent.Anim.OnServerEvent:Connect(function(plr)
	local AttackDist = 10 --If a player is further than this value, they won't be damaged.
	
	for i,v in pairs(game.Players:GetChildren()) do --Loops through all players.		
		if v.Character ~= nil and plr.Character ~= nil and v ~= plr then --Checks if there's a character
			local pRoot = plr.Character:FindFirstChild("HumanoidRootPart")
			local vRoot = v.Character:FindFirstChild("HumanoidRootPart")
			
			if pRoot and vRoot and (plr.Character.HumanoidRootPart.Position - v.Character.HumanoidRootPart.Position).Magnitude < AttackDist then --Checks if the Root parts exist and if the player is in range.
				local Hum = v.Character:FindFirstChild("Humanoid")
				
				if Hum and Hum.Health > 0 then --Checks if player is alive.
					Hum.Health = Hum.Health - 100 --Change "100" to the damage the player should take.
				end
				
				--Stuff that happens when a player is in range.
			end
		end
	end
end)
2 Likes

Thanks you guys alot!
Really the help here is much better than in stack :smiley:

Thanks you! :smiley: I will use it in my project, really thanks

Hello, can I ask you a question?

How could I use this code but for the touch to other players to be generated with the left arm and the right arm?

What if touching any other player with the “left arm or right arm” decrease life to others players… please could you help me with that? :frowning:

You want to damage other players only if the attacker is touching them with one of their arms?

Yeah exactly, how i could do that?

There are multiple ways to do this, but if you want it to be activated via RemoteEvent, I think Region3 could work well. Be sure to add a few checks to prevent exploiters from abusing the Remote!

local Damage = 10 --Damage dealt by an attack

script.Parent.Parent.Anim.OnServerEvent:Connect(function(plr)
	if plr.Character ~= nil then --Checks if player has a character
		local pHum = plr.Character:FindFirstChild("Humanoid")
		local pArmL = plr.Character:FindFirstChild("Left Arm") or plr.Character:FindFirstChild("LeftHand")
		local pArmR = plr.Character:FindFirstChild("Right Arm") or plr.Character:FindFirstChild("RightHand")
		local Damaged = {} --List of Humanoids that were already damaged. Prevents victims from taking damage twice.
		
		if pHum and pHum.Health > 0 and pArmL and pArmR then --Checks if attacker is alive
			local RegionL = Region3.new(pArmL.Position - (pArmL.Size/2),pArmL.Position + (pArmL.Size/2)) --Creates a Region around the left arm
			local RegionR = Region3.new(pArmR.Position - (pArmR.Size/2),pArmR.Position + (pArmR.Size/2)) --Creates a Region around the right arm
			
			for i,v in pairs(game.Workspace:FindPartsInRegion3(RegionL,nil,math.huge)) do --Loops through parts that are touching the left arm
				local vHum = v.Parent:FindFirstChild("Humanoid") or v.Parent.Parent:FindFirstChild("Humanoid")
				
				if vHum and vHum.Health > 0 and vHum ~= pHum and not table.find(Damaged,vHum) then --Checks if humanoid is alive and if it has already been damaged
					table.insert(Damaged,#Damaged + 1,vHum) --Adds the humanoid to the Damaged table
					vHum.Health = vHum.Health - Damage --Damages humanoid
					--Stuff that happens when hit with the left arm
				end
			end
			
			for i,v in pairs(game.Workspace:FindPartsInRegion3(RegionR,nil,math.huge)) do --Loops through parts that are touching the right arm
				local vHum = v.Parent:FindFirstChild("Humanoid") or v.Parent.Parent:FindFirstChild("Humanoid")
				
				if vHum and vHum.Health > 0 and vHum ~= pHum and not table.find(Damaged,vHum) then
					table.insert(Damaged,#Damaged + 1,vHum)
					vHum.Health = vHum.Health - Damage
					--Stuff that happens when hit with the right arm
				end
			end
		end
	end
end)
1 Like