local tool = script.Parent local name = script.Parent.Parent.Parent local descend = workspace:GetDescendants() tool.Activated:Connect(function() for i = 1, #descend do local descendant = descend[i] if descend.Name == name then descend.Humanoid.Health = 100 end end end)
Erm, I'm quite confused why it doesnt change my health to 100, this is a script, by the way.
Sorry, it's 12am and i was working on this game for like 8 hours straight and my brain is garbage right now, can anyone tell me what mistakes I've made in that script?
Hello! Someone downvoted my answer, even though I tested and it does work. This code includes a few extra features such as the transition between low health and 100 being smooth and there is a decently in-depth explanation of the code so that you can learn. I put a lot of effort into writing and testing this answer, so if this does help you, please accept it.
I found a few bugs, such as the variable name
just referencing an instance, not the actual name, and also only modifying the property health on the client (remote events needed). I decided to do a rewrite that's simple, so here's my version:
LocalScript In The Tool
local tool = script.Parent tool.Activated:Connect(function() game.ReplicatedStorage.HealCharacter:FireServer() end)
Now create a RemoteEvent in ReplicatedStorage and call it "HealCharacter". Then create a script (server script/normal script) in ServerScriptService
Script In ServerScriptService
game.ReplicatedStorage.HealCharacter.OnServerEvent:Connect(function(player) local hum = player.Character:WaitForChild("Humanoid") local Info = TweenInfo.new(1) local Tween = game:GetService("TweenService"):Create(hum,Info,{Health=100}) Tween:Play() end)
Everything has been tested and it works. If you want my .rblx file that I tested in, contact me on discord and I can send it to you. Green#2605
Warning: Make sure you have a Handle part if the property 'RequiresHandle' of the Tool is enabled. This will not work if the player's health is decreased locally. Which means that the when the health is lowered in the first place, it must be a server script or involve Remote Events. If you need any help with this last part, or are confused, feel free to ask me in the comments of this answer.
I really hope this helps you and you learned something!
-ProqrammedGreen
Why can't you just go
local tool = script.Parent local player = tool.Parent.Parent local char = player.Character or player.CharacterAdded:Wait(1) local humanoid = char:WaitForChild("Humanoid") tool.Activated:Connect(function() humanoid.Health = humanoid.Health + 100 end)
Am I missing something?
I believe that this does not work because the "name" variable probably got the Backpack or some other Player Object instead of the actual Character. Remember, scripts in tools still function as normal even when they're not equipped.
WaitForChild will not work either, as it will wait for the Backpack or some other Player Object to appear in the Workspace, which is a scenario that will never happen.
So what is the solution? Use FindFirstChild or FindFirstChildOfClass to check the Humanoid. It will check if the Character/Humanoid is there in the first place when you activate the tool. And it will definitely please the trouble of having to check every individual descendant of the Workspace.
IMPORTANT NOTE: It is also recommended that you add a handle to your tool if the boolean RequiresHandle is true.
local tool = script.Parent tool.Activated:Connect(function() local Humanoid = script.Parent.Parent:FindFirstChildOfClass("Humanoid") --FindFirstChildOfClass will check any other objects classified as "Humanoid," just incase if there is another object that is named "Humanoid." if Humanoid then -- If the script got an object classified as "Humanoid" then: Humanoid.Health = Humanoid.Health + 100 end end)