Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to make character heal using animation and keydown function?

Asked by 7 years ago

Hello everyone, I am new to scripting, and I was looking for some help of this potion animation I was doing.

Basically, I created a Local Script where if your health is below 50, you can press a certain key which will play an animation, and restore health. For some reason, it's not working, so I was looking for some help.

Local Script:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local animation = Instance.new("Animation")
animation.AnimationId = "http://www.roblox.com/Asset?ID=734045225"
local humanoid = character:FindFirstChild('Humanoid')
local HealthGain = 20


if humanoid then
    local currentHealth = humanoid.Health
    local newHealth = currentHealth + HealthGain
    humanoid.Health = newHealth
end

if player.Character.Humanoid.Health < 50 then
mouse.KeyDown:connect(function(key)
    if key == "h" then 
        animation.Play()
        wait (5)
        animation.Stop()

if humanoid then
local currentHealth = humanoid.Health
local newHealth = currentHealth + HealthGain
humanoid.Health = newHealth

end     

    end

end`)

I can't quite figure out the problem with my script, but I have a feeling it has something to do with the keydown function, or the animation functions.

I'm quite new to scripting, so much help is appreciated!

1 answer

Log in to vote
0
Answered by
saucyP 7
7 years ago

Line 05, you didn't define what character was. The answer is on Line 03.

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local character = player.Character
local animation = Instance.new("Animation")
animation.AnimationId = "http://www.roblox.com/Asset?ID=734045225"
local humanoid = character:FindFirstChild('Humanoid')
local HealthGain = 20


if humanoid then
    local currentHealth = humanoid.Health
    local newHealth = currentHealth + HealthGain
    humanoid.Health = newHealth
end

if player.Character.Humanoid.Health < 50 then
mouse.KeyDown:connect(function(key)
    if key == "h" then 
        animation.Play()
        wait (5)
        animation.Stop()

if humanoid then
local currentHealth = humanoid.Health
local newHealth = currentHealth + HealthGain
humanoid.Health = newHealth

end     

    end

end`)

Ad

Answer this question