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

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

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

✓ Accepted answer

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)

4 comments

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 :/

0

Oh, and I forgot to mention, the output is giving me this error: attempt to index nil with 'Character'

0

do local character = player.Character or player.CharacterAdded:Wait() on line 4

0

sometimes the character doesn’t load yet that’s why add that on line 4

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