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

How would I go about healing the player using a tool?

Asked by 4 years ago

I'm working on a healing spell, and I cant seem how to figure out how to heal the player. When I click, it prints what I want it to, but doesn't heal. I think I probably am missing a few things in my code. I tried the wiki but cant seem to find the solution to my problem. here is my code

local tool = script.Parent
local field = nil
local character = tool.Parent

local human = character:FindFirstChild("Humanoid")

function heal()
    if (human ~=nil) then
        human.Health = human.Health + 25
    end

    end



tool.Activated:Connect (function()
    heal()
    print ("healed")
end)

I'm not asking for anyone to rewrite it, just point out what I messed up/missed out. Thanks in advance! :)

1 answer

Log in to vote
0
Answered by 4 years ago

The problem is in line 3, when the script is first run, the tools parent will be the backpack. The tools parent won't be the character until you equip the tool. Just do this below

local tool = script.Parent
local field = nil
local player = game.Players.LocalPlayer 
local character = player.Character --The problem was here. I changed it for you

local human = character:FindFirstChild("Humanoid")

function heal()
    if (human ~=nil) then
        human.Health = human.Health + 25
    end

    end



tool.Activated:Connect (function()
    heal()
    print ("healed")
end)

0
hmm, Doesn't seem to do anything now. I tried putting this into a local script and got the same results. I might have to just redo it, idk :/ Telasim0 6 — 4y
0
Oh, and I forgot to mention, the output is giving me this error: attempt to index nil with 'Character' Telasim0 6 — 4y
0
do local character = player.Character or player.CharacterAdded:Wait() on line 4 Grazer022 128 — 4y
0
sometimes the character doesn’t load yet that’s why add that on line 4 Grazer022 128 — 4y
Ad

Answer this question