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

Why does it not heal me when I activate the tool?

Asked by 4 years ago
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?

0
currently working an an answer proqrammed 285 — 4y
0
It's not healing because the variable is referencing the object, not the name. niroqeo 123 — 4y
0
please read/consider both our answers/comments proqrammed 285 — 4y

3 answers

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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

0
Why do you need remote events? niroqeo 123 — 4y
0
Remote Events are needed in order to make it so all the players see that the player is healed. Without remote events, only one player would see the changes. Remote events also prevent against exploiters. proqrammed 285 — 4y
0
I know what remote events are. I'm just asking. Also, why do are you using TweenService? niroqeo 123 — 4y
0
I tweened the health to 100 because it makes the transition between low health to 100 smooth and is faster than a for loop or anything else. It just adds some jazz to it proqrammed 285 — 4y
View all comments (2 more)
0
Hi ProqrammedGreen, I have a question. Why is the TweenInfo.new(1)? I haven't discovered this way of using tweens yet. AcrylixDev 119 — 4y
0
I mean like why is there a 1 inside the parameters of TweenInfo? And in the created tween, the 2nd paramameter, which is usually tween info like Enum.EasingStyle, it's just TweenInfo.new(1) AcrylixDev 119 — 4y
Ad
Log in to vote
0
Answered by
niroqeo 123
4 years ago

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?

0
There's alot more to it. proqrammed 285 — 4y
0
I tested it! I'm assuming you either changed the health on the client, or you didn't insert a handle and forgot to disable "RequiresHandle" niroqeo 123 — 4y
0
Sorry, I mean it only functions on the client, which means only the client will see the changes. So in case you want other players, to see your healing and the game to be secure, Remote Events are needed (like in my answer). My answer also has a few other features just for smoothness proqrammed 285 — 4y
0
Tool.Activated can be called on the server as well. I used the Server-Client thingy and it showed that I was healed. niroqeo 123 — 4y
View all comments (8 more)
0
I just wanted to teach Remote Events, along with tweening. My preference is to teach the most possible in a single answer, while making it simple. I also put a lot of effort into my answer.  proqrammed 285 — 4y
0
Please try to test this script, but add a handle and make it's parent the tool. It will work. niroqeo 123 — 4y
0
I know it will work, I just think that my answer has more to offer and teaches more to readers. proqrammed 285 — 4y
0
Well, please remove the downvote.. niroqeo 123 — 4y
0
I didn't put that there, I'll give it an upvote so it's back to 0. And if you downvoted mine can you remove it? proqrammed 285 — 4y
0
Oh, sorry. Also, I don't have enough reputation to upvote. niroqeo 123 — 4y
0
Did you downvote it though? If you can downvote it means you can remove it, so can you please remove it? And also, you can upvote, because you have more than 25 repuation. proqrammed 285 — 4y
0
Oh, ok. I've upvoted yours. niroqeo 123 — 4y
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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)

Answer this question