Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Health drain power wont work why?

What it's supposed to do is when you hover your mouse over a player or a dummy it will drain their health but it doesn't work can I get help?

game.Players.LocalPlayer:GetMouse().KeyDown:Connect(function(key)
	if key == "e" then
				local Player = game.Players.LocalPlayer
				local Mouse = Player:GetMouse()
				local humanoid = script.Parent.Humanoid
				local uppertorso = workspace.script.Parent.HumanoidRootPart
				--------------
				--------------
				wait()
				uppertorso.CFrame=uppertorso.CFrame*CFrame.new(0,0,0)*CFrame.Angles(-.5,0,math.random(0,0))
				wait()
				repeat
					humanoid.PlatformStand = true
					humanoid.JumpPower = 0
					humanoid.Health = humanoid.Health - 2
					humanoid.PlatformStand = true
					wait(0.3)
					humanoid.PlatformStand = true
				until humanoid.Health <= 50
				humanoid.PlatformStand = false
				humanoid.JumpPower = 50
				wait()
			
 	end
end)

1 answer

✓ Accepted answer

Well, you have some problems and missing components.

First, this is all local so you will only be damaging a player on your screen, but in reality, nothing is happening to them on the server's side.

I also don't see how you're getting a hold of the targeted players humanoid so I also believe you're missing that. I'll walk you through how to make it on the server's side, which will require a RemoteEvent and a ServerScript to be added.

Please keep in mind that I took the time to make a new LocalScript, along with a server script to guide you to what your goal is, so anything unfamiliar to you will be expected.

  1. Let's add a RemoteEvent in ReplicatedStorage and name it HealthDrainEvent

Now, the LocalScript in StarterCharacterScripts will contain: The LocalScript has been edited to use a key for it to activate instead, using UserInputService to retrieve the input from the player

local RP = game:GetService("ReplicatedStorage")
local UIS = game:GetService("UserInputService")

local HealthDrainDevent = RP:WaitForChild("HealthDrainEvent")

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local draining = false
local cooldown = 1


UIS.InputBegan:Connect(function(input, processed)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.E then --Change to whatever key you want
			local target = mouse.Target

			if target.Parent.Humanoid or target.Parent.Parent.Humanoid then
				local TargetHumanoid = target.Parent.Humanoid or target.Parent.Parent.Humanoid

				if TargetHumanoid ~= player.Character.Humanoid then
					print("Enemy humanoid detected, initiating doom.")
					draining = true
					while draining == true do 
						HealthDrainDevent:FireServer(TargetHumanoid)
						wait(cooldown)
					end
				end
			end
		end	
	end
end)

UIS.InputEnded:Connect(function(input, processed)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.E then --must correspond to the key you use to active the process
			draining = false
		end
	end
end)

and finally, add a ServerScript in ServerScriptService and put in the following:

local RP = game:GetService("ReplicatedStorage")

local HealthDrainEvent = RP.HealthDrainEvent

HealthDrainEvent.OnServerEvent:Connect(function(player, targetedHumanoid)
	if targetedHumanoid.Health > 50 then
		targetedHumanoid.PlatformStand = true
		targetedHumanoid.JumpPower = 0
		targetedHumanoid:TakeDamage(2) -- or whatever damage you want
	else
		targetedHumanoid.PlatformStand = false
		targetedHumanoid.JumpPower = 50
	end
end)

What you've been trying to achieve should now be working, make sure to alter it to your needs, for example, if you want them to press a key in order for it to activate instead of clicking. At the end of the day I hope this helps you out and if you have questions please add a comment below this answer.

3 comments

0

sorry im pretty new to scripting and you're so helpfull tysm :3

0

but what if i want it to be a keybind?

0

I'll edit it so it can do that.

This discussion is preserved as it was. New answers, comments, and votes are closed.