Help With Creating Health Potion

I’m a beginner and i was trying to make this health potion tool that heals when the player use it and also play an idle animation/use animation( that’s why i used the function to check if the player is holding the tool)

i’m struggling with giving player health, i tested the code with Print() and every line runs, the problem is when i take damage(i use a damage block from the toolbox for testing) it shows like my character was Healed but if i take damage again it adds to the original damage i have taken, by what i’m seeing the healing is just visual and dont change the Health

i tried to find about it on te roblox documentation and on the devforum but i couldn’t figure out what is causing this

i used a local script and then changed to a server script but the local dont heal and the server script gives me this error:

Players.pietroGDBR123.Backpack.Tool.Script:9: attempt to index nil with 'Character'  -  Server - Script:9

this is my script:

local item = script.Parent
local healAmount = 50
local player = game:GetService("Players").LocalPlayer

local function onEquipped(_Mouse)
	print("item equipped")

	local function onActivation()
		local character = player.Character or player.CharacterAdded:Wait()
		local humanoid = character:FindFirstChild("Humanoid")
		if humanoid then
			local health = humanoid.Health
			if health < 100 then -- se a vida for menor que 100

				player.Character.Humanoid.Health += healAmount
				print("Healed!")
			else
				print("your health is full!")
			end
		end

	end

	item.Activated:Connect(onActivation)
end
item.Equipped:Connect(onEquipped) -- quando equipar, executa a funçãoprint("Hello world!")

screenshot of my tool:

i dont have any event or other script only this one for the tool

1 Like

Hello. This is on a server script, so you can’t access the LocalPlayer.
You are also connecting a new activation on each equip. Here is your updated script:

local Tool = script.Parent
--
local HEAL_AMOUNT = 50
--
local currentCharacter: Model? = nil

--[[When equipped, get the character that is holding.]]
local function OnEquipped()
	print("item equipped")
	currentCharacter = Tool.Parent
end

--[[When unequipped, no one is holding anymore.]]
local function OnUnequipped()
	currentCharacter = nil
end

--[[When activated, heal current holding if any.]]
local function OnActivated()
	if not currentCharacter then
		return
	end
	local humanoid: Humanoid? = currentCharacter:FindFirstChild("Humanoid")
	if not humanoid then
		return
	end
	if humanoid.Health < humanoid.MaxHealth then
		humanoid.Health += HEAL_AMOUNT
		print("Healed!")
	else
		print("your heal is full!")
	end
end

--
Tool.Equipped:Connect(OnEquipped)
Tool.Unequipped:Connect(OnUnequipped)
Tool.Activated:Connect(OnActivated)
1 Like

it works! thanks can i ask you some questions? i’m trying to learn scripting and there’s some stuff i dont understand so well:

1 - local currentCharacter: Model? = nil

2 - --[[When unequipped, no one is holding anymore.]]
local function OnUnequipped()
currentCharacter = nil
end

Regarding these parts: what does ‘Model?’ mean? I couldn’t find it in the Roblox documentation. Also, for the second one, what is the difference between using this function to detect when no one is holding the tool vs only using the ones that detect when a player is holding/clicking? Is it something I always need to use with ‘detect functions’, or only in certain situations? For example, if I want the tool to be destroyed after use, should I remove it and replace it with Destroy(), or not?

I am pretty sure it is because when you equip a tool it gets parented to the character hence why it says in the on equip function the currency character is tool.parent. You can see this by having a tool in starter character, then play the game, then go to workspace and find your character. When you find your character open it up and equip the tool, it should show up there, and when you un equip the tool it goes away. Sorry if this does not make sense :sweat_smile: good luck! Lmk if you need anything. And regarding the model? I think that is just a personal style, because I have never done something like that.

1 Like

local currentCharacter: Model? = nil

  • We know this variable will allocate a model, but we don’t have that model yet. When we do variable: Model, it will get auto-completed as if it was a model, improving the process of writing code. It is purely for convenience while writing and has no effects during runtime.
--[[When unequipped, no one is holding anymore.]]
local function OnUnequipped()
currentCharacter = nil
end

This is purely a preference. As you said, you could always just check who is holding the tool the moment it is activated.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.