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

Attempt to index local 'healthAmount' (a number value)?

I'm trying to a make a custom health gui, and I get this error. Help? (I already tried game.Players.LocalPlayer.Character.Humanoid.Health, same error.)

Use both scripts if needed:

--LOCAL SCRIPT--

local RS = game:GetService("ReplicatedStorage")
local remotef = RS.RemoteFunctions:WaitForChild("Change3")

while true do
	wait(.0001)

local MV = remotef:InvokeServer(workspace:FindFirstChild(script.Parent.Parent.Parent.Parent.Parent.Parent.Name):WaitForChild("Humanoid").Health) -- The problem/error
script.Parent.Text = "[H] HEALTH: "..MV
end
--SERVER SCRIPT--

function cV (player,healthAmount)
	while true do
		wait(.0001)
	return healthAmount.Value
	end
end
	
	game.ReplicatedStorage:WaitForChild("RemoteFunctions").Change3.OnServerInvoke = cV

3 answers

this is because you are trying to concatenate a number value, try a tostring() function for that

--LOCAL SCRIPT--

local RS = game:GetService("ReplicatedStorage")
local remotef = RS.RemoteFunctions:WaitForChild("Change3")

while true do
    wait()

local MV =remotef:InvokeServer(workspace:FindFirstChild(script.Parent.Parent.Parent.Parent.Parent.Parent.Name):WaitForChild("Humanoid").Health) -- The problem/error
if type(MV) == "number" then
	local strvMV = tostring(MV)
	script.Parent.Text = "[H] HEALTH: "..strMV	
elseif type(MV) == "string" then
	script.Parent.Text = "[H] HEALTH: "..MV
end

end

So the script above checks if MV is a number is a string or a number, if it is a string, it concatenates right away, else it turns it into a string and then concatenates it.

function cV (player,healthAmount)
    while true do
        wait()
    return healthAmount
    end
end

    game.ReplicatedStorage:WaitForChild("RemoteFunctions").Change3.OnServerInvoke = cV

the server script seems fine, although you really don't need that many while loops

1 comment

0

You can concatenate numbers in lua, try "print('hey' .. 5)" in the command line.

You have

healthAmount.Value

If healthAmount is the same as Humanoid.Health, and not a NumberValue Instance then you don't need to add the ".Value".

#Issues:#

  1. TOO MANY LOOPS
  2. You don't need a server script for this
  3. There are simpler ways to do this

#Steps:#

  1. Place a LocalScript into StarterCharacterScripts
-- local script only

local plr = game.Players.LocalPlayer
local Char = script.Parent
local Humanoid = Char:WaitForChild('Humanoid')
local PlayerGui = plr:WaitForChild('PlayerGui')
local TextLabelToDisplayHealth = -- do ur thing here

Humanoid.HealthChanged:Connect(function(h)
	TextLabelToDisplayHealth.Text = h -- h is = to current health
	-- or 
	TextLabelToDisplayHealth.Text = '[H] Health: '..h
	-- or
	TextLabelToDisplayHealth.Text = '[H] Health: '..h..' / '..Humanoid.MaxHealth
end)

Use this code, modify to your favoring.

Hopefully this helped you

Best of luck developer.

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