Getting humanoid from local player not working?

I have been trying to fix this bug for well over an hour and I am completely stumped. (Its probably something silly breaking it) The problem is on line 8 with the playerHumanoid parameter from function. The error is: Players.Its4Realzies.Backpack.Blessed Medkit.HealScript:8: attempt to index nil with ‘Name’

Tool = script.Parent

–Heal function for both user and most hurt teammate
local function healPlayer(playerHumanoid, healAmount)
local healthAdded = 0

print(playerHumanoid.Name)
local healParticle = script.HealEffects:Clone()
healParticle.Enabled = true
healParticle.Parent = playerHumanoid.Parent.Torso

repeat
	if playerHumanoid.MaxHealth <= playerHumanoid.Health + healAmount/10 then
		playerHumanoid.Health = playerHumanoid.maxHealth
	else
		playerHumanoid.Health = playerHumanoid.Health + healAmount/10
		healthAdded = healthAdded + healAmount/10
	end
	wait(0.1)
until healthAdded >= healAmount or playerHumanoid.Health == playerHumanoid.maxHealth 

healParticle.Enabled = false

end

function Heal()

--Play sound
Tool.Handle.Fire.PlayOnRemove = true
Tool.Handle.Fire:Destroy()

local localPlayer = game:GetService("Players").LocalPlayer
--Heals user
local human = localPlayer.Character:FindFirstChild("Humanoid")
if (human ~= nil) then
	healPlayer(human, 100)
end

local mostInjuredHealth = 100
local mostInjuredHumanoid = nil
--Find most injured teammate
for _,v in pairs(game.Players:GetChildren()) do
	if v.Character ~= nil and v.TeamColor == localPlayer.TeamColor and 0 < v.Character.Humanoid.Health and v.Character.Humanoid.Health < mostInjuredHealth then
		mostInjuredHealth = v.Character.Humanoid.Health
		mostInjuredHumanoid = v.Character:FindFirstChild("Humanoid")
	end
end
--Heal most injured teammate
healPlayer(mostInjuredHumanoid, 100)

--Destroys tool mesh 
if workspace:FindFirstChild(Tool.PlayerNameValue.Value) ~= nil then 
	workspace:FindFirstChild(Tool.PlayerNameValue.Value):destroy()
end
--Destroys Tool
Tool.Parent = nil

end

function onActivated()

if not Tool.Enabled then
	return
end
Tool.Enabled = false

Heal()

end

script.Parent.Activated:connect(onActivated)

1 Like

I would be doing it on the server.

1 Like

Why and how would this change the outcome?

It wont be delayed by replication. As well as if its going to effect to other players its over all a way better. Since if your regening health “locally”, the server will still think your health is say at 10 so when a player kills you, it will say you have died on server even though your alive “locally” in your view. Over all its a lot better to being using a normal script for humanoid. I mean this is just my recommendation. You can correct me if im wrong though.

2 Likes

Try waiting for the Humanoid instead of using FindFirstChild.
local human = localPlayer.Character:WaitForChild("Humanoid")

I’ve tried FindFirstChild just . and WaitForChild I think ScriptedPoptart is on to something

Is there a way I can do it without it breaking in a local script?

My bad. I didn’t notice it was in a function so it wouldn’t have really mattered.
Anyways, I suggest healing players on the server like ScriptedPoptartz said, because on a local script it wouldn’t really heal them - only show like they have more health.

1 Like

Its still not working and it apears to be the same error. Here is the model slap it in starterGui can see if you can find the problem :disappointed_relieved: Blessed Medkit - Roblox

LocalPlayer does not work on ServerScripts.

That is why the “Name” and “Humanoid” + “Character” won’t work as you are using LocalPlayer.

LocalPlayer works only on LocalScripts.

Server and Client, short introduction


First of all, it’s important to address the differences between the client and server.

→ An example guide about this can be found here:
https://www.cloudflare.com/learning/serverless/glossary/client-side-vs-server-side/
Roblox Client-Server Model

==

To make it short, the server is the one in control of everything, and all the clients that are connected to the server notify the server abut any changes.

The link attached above will take point in web development, but server / client side applies to most applications in the real world. In Roblox and general game development, the client is the player (and their computer). For example, if you’re playing a game such as Arsenal, everything you see and do on your computer is done by you, the client.

The server is the one calling all the shots and is basically the “master” of the game running. If you shoot, walk, run, reload, do anything in a multiplayer game, you notify the server. Since the server is its own environment it does not have access to all methods and properties as if it was the client. For example: If you put a LocalScript under the client, it can access the property “LocalPlayer” which would be the Player instance, whereas the server would only be getting nil (nothing).

Why does this matter? If you write code that’s only going to work on the server, and put it under the client, then things are going to break, and vice-versa if you code something that will only work on the client and put that on the server. It’s not going to work, or, it will give unwanted side-effects, also known as bugs.

Your issue


First of all, you have a regular server script under the tool (the tool is used directly by the client). Remember what I said what would happen - if you create something that should be placed on the server, but instead put it on the client? It won’t work.

image

In addition, you are healing the client locally and not on the server - which you definitely should do.

Let’s say you have 80HP, since the server is in control of everything, if you heal yourself only locally and don’t notify the server about this, even if it says 100HP on your screen (and the client thinks it has 100HP), you still only have 80HP. Why? Because you’re updating your health value on the client, not on the server.

The Final Fix


You should rewrite your code to comply with the client-server model as that will ensure your game is working properly and for everyone. I will allow myself to write some demo code for you and your goal.

Here’s the setup:
image

Our goals:

  • When the tool is activated, play heal effects and ask the server to heal us

Here’s the demo I wrote up so far, feel free to adjust to your usecase:
client-server-model help.rbxl (35.5 KB)

1 Like

Why the semicolon of the variables at the top of the activation listener? Is that required for it to work? If so what do they do?

Sorry for all the questions, but why this:

– Waiting for 1 second before removing the effect
delay(2, function ()
clonedHealParticles.Enabled = false;
clonedHealParticles:Destroy();
end);

Couldn’t it just be done with
clonedHealParticles.Enabled = false
game.Debris:AddItem(clonedHealParticles, 5)

Sorry for the delay, I went to bed after answering lol.

Semicolons are just a habit of writing other code, and they don’t really do anything, most of the time. Semicolons are in Lua just interpreted as whitespace (“nothing”).

I wrote that just for demonstration purposes, some people claim DebrisService is unreliable so I just used a delay, which waits the given seconds before executing the function provided. As you wrote, you could just write

DebrisService:AddItem(clonedHealParticles, 5);

… and that will remove the cloned heal particles effect after 5 seconds.