What am I trying to do? I’m trying to make any player heal normal 1 just like any game in a separated script that I made and it worked, yay, but, now, I am trying to make a keyboard holding button that is “F” makes the player heal 5 but, my script didn’t work as expected. Could you scripters tell me where I messed up in the script? Thanks! <3
local REGEN_RATE = 1/100 -- Regenerate this fraction of MaxHealth per second.
if input.KeyCode == Enum.KeyCode.F then
local REGEN_STEP = 5 -- Wait this long between each regeneration step.
--------------------------------------------------------------------------------
local Character = script.Parent
local Humanoid = Character:WaitForChild'Humanoid'
--------------------------------------------------------------------------------
while true do
while Humanoid.Health < Humanoid.MaxHealth do
local dt = wait(REGEN_STEP)
local dh = dt*REGEN_RATE*Humanoid.MaxHealth
Humanoid.Health = math.min(Humanoid.Health + dh, Humanoid.MaxHealth)
end
Humanoid.HealthChanged:Wait()
end
Could it be all in a LocalScript? If the player holds the keyboard button “F” the REGEN_STEP changes to 5 then when the player unholds the keyboard button “F” the REGEN_STEP returns to 1?
local ContextActionService = game:GetService("ContextActionService")
local Players = game:GetService("Players")
--------------------------------------------------------------------------------
-- Gradually regenerates the Humanoid's Health over time.
local REGEN_RATE = 1/100 -- Regenerate this fraction of MaxHealth per second.
local REGEN_STEP = 1 -- Wait this long between each regeneration step.
--------------------------------------------------------------------------------
local Character = script.Parent
local Humanoid = Character:WaitForChild'Humanoid'
--------------------------------------------------------------------------------
while true do
while Humanoid.Health < Humanoid.MaxHealth do
local dt = wait(REGEN_STEP)
local dh = dt*REGEN_RATE*Humanoid.MaxHealth
Humanoid.Health = math.min(Humanoid.Health + dh, Humanoid.MaxHealth)
end
Humanoid.HealthChanged:Wait()
end```?
-- Dependencies
local ContextActionService = game:GetService("ContextActionService")
local Players = game:GetService("Players")
--------------------------------------------------------------------------------
local beingHeld = false
function handleAction(actionName, inputState, inputObj)
if inputState == Enum.UserInputState.Begin then
-- Gradually regenerates the Humanoid's Health over time.
local REGEN_RATE = 1/100 -- Regenerate this fraction of MaxHealth per second.
local REGEN_STEP = 5 -- Wait this long between each regeneration step.
local Character = script.Parent
local Humanoid = Character:WaitForChild'Humanoid'
beingHeld = true
elseif inputState == Enum.UserInputState.End
-- Gradually regenerates the Humanoid's Health over time.
local REGEN_RATE = 1/100 -- Regenerate this fraction of MaxHealth per second.
local REGEN_STEP = 1 -- Wait this long between each regeneration step.
local Character = script.Parent
local Humanoid = Character:WaitForChild'Humanoid'
beingHeld = false
end
end
while true do
while Humanoid.Health < Humanoid.MaxHealth do
local dt = wait(REGEN_STEP)
local dh = dt*REGEN_RATE*Humanoid.MaxHealth
Humanoid.Health = math.min(Humanoid.Health + dh, Humanoid.MaxHealth)
end
Humanoid.HealthChanged:Wait()
end
--------------------------------------------------------------------------------
-- Bind action
CAS:BindAction("pressingF", handleAction, false, Enum.KeyCode.F
Okay, please review my code. I worked hard making it small, and good to understand.
Code:
-- Dependencies
local ContextActionService = game:GetService("ContextActionService")
local Players = game:GetService("Players")
--------------------------------------------------------------------------------
local beingHeld = false
function handleAction(actionName, inputState, inputObj)
if inputState == Enum.UserInputState.Begin then
-- Gradually regenerates the Humanoid's Health over time.
local REGEN_RATE = 1/100 -- Regenerate this fraction of MaxHealth per second.
local REGEN_STEP = 5 -- Wait this long between each regeneration step.
--------------------------------------------------------------------------------
local Character = script.Parent
local Humanoid = Character:WaitForChild'Humanoid'
--------------------------------------------------------------------------------
beingHeld = true
elseif inputState == Enum.UserInputState.End
-- Gradually regenerates the Humanoid's Health over time.
local REGEN_RATE = 1/100 -- Regenerate this fraction of MaxHealth per second.
local REGEN_STEP = 1 -- Wait this long between each regeneration step.
--------------------------------------------------------------------------------
local Character = script.Parent
local Humanoid = Character:WaitForChild'Humanoid'
--------------------------------------------------------------------------------
beingHeld = false
end
end
--------------------------------------------------------------------------------
-- Bind action
CAS:BindAction("pressingF", handleAction, false, Enum.KeyCode.F)
--------------------------------------------------------------------------------
while true do
while Humanoid.Health < Humanoid.MaxHealth do
local dt = wait(REGEN_STEP)
local dh = dt*REGEN_RATE*Humanoid.MaxHealth
Humanoid.Health = math.min(Humanoid.Health + dh, Humanoid.MaxHealth)
end
Humanoid.HealthChanged:Wait()
end
So ContextActionService must be used in a Local Script, which unfortunately also means changing the Humanoid’s health isn’t as easy as:
humanoid.Health = humanoid.Health + 5
Local Scripts cannot change these types of values because it would allow hackers to easily manipulate their own health and ruin the game for everyone by being immortal.
This is why Filtering Enabled exists (I recommend reading up on it)
If you want to add health to your humanoid, you must Fire a RemoteEvent to the Server, where a Server Script that listens to the RemoteEvent adds health to the player’s humanoid for you.
What @MrLegendaryDoge just posted looks totally viable and likely a better solution than mine. I just converted an old repair vehicle script of mine into this:
-- Local Script in StarterPlayerScripts:
-- Make sure to have a RemoteEvent in ReplicatedStorage called "RemoteEvent"
local player = game.Players.LocalPlayer
local character = player.Character
if not character or not character.Parent then
character = player.CharacterAdded:wait()
end
local humanoid = character:WaitForChild("Humanoid")
local contextActionService = game:GetService("ContextActionService")
local RemoteEvent = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent")
local REGEN_RATE = 10/100
local REGEN_STEP = 5
local holding = true
local function healPlayer(actionName, inputState, inputObject)
if inputState == Enum.UserInputState.Begin then
holding = true
while holding do
RemoteEvent:FireServer(humanoid.MaxHealth * REGEN_RATE)
wait(REGEN_STEP)
end
elseif inputState == Enum.UserInputState.End then
holding = false
end
end
contextActionService:BindAction("HealPlayer", healPlayer, false, Enum.KeyCode.F)
-- ServerScript in ServerScriptService
local RemoteEvent = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent")
local function healPlayer(player, regenAmount)
local character = player.Character
if not character or not character.Parent then
character = player.CharacterAdded:wait()
end
local humanoid = character:WaitForChild("Humanoid")
humanoid.Health = humanoid.Health + regenAmount
if humanoid.Health > humanoid.MaxHealth then
humanoid.Health = humanoid.MaxHealth
end
end
RemoteEvent.OnServerEvent:Connect(healPlayer)
Please note that what @MrLegendaryDoge and I have done for you is sort of above and beyond what repliers should be doing on the Support DevForum, in the future, I don’t recommend asking for us to “write all the code in one reply.” What repliers should be doing in that situation is give resources to help you write the code by yourself. Consider yourself lucky
Now I Seriously recommend going through what we have given you and test and try to understand it yourself, you can learn a lot!
Also read up on RemoteEvents and FilteringEnabled, it’s vital to Roblox game making.