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

My script isn't working I also have the error. Any fixes?



local player = game.Players.LocalPlayer


if game.Workspace[player.Name].Humanoid.Health == 0 then
	script.Parent.Visible = false
end

Error - Humanoid is not valid member of model

2 answers

✓ Accepted answer

The script instantly runs as soon as the script is loaded, and the character and humanoid takes a bit longer to load, therefore the character and humanoid won't exist by the time the script runs.

Also, I'm guessing you're trying to detect when the player dies, so just use humanoid.Died instead of Humanoid.Health.

Example:

local plr = game.Players.LocalPlayer --assigning the player to the variable 'plr'
local char = plr.Character or plr.CharacterAdded:Wait() --assigning the character to the variable 'char', and if doesn't exist by the time the script runs it'll run the 'or' statement, aka wait for the character to be added
local hum = char:WaitForChild("Humanoid") --It will wait for the humanoid to be loaded, and assign it to the variable 'hum'

hum.Died:Connect(function() --When the humanoid dies, run this function:
	script.Parent.Visible = false
end)

1 comment

0

Oh thank you

What you could do is: as the script runs as soon as it is loaded which means the the humanoid might not have a chance to be loaded yet.

local player = game.Players.LocalPlayer
local Character = player.Character or player.CharacterAdded:wait()


if Character and (Character.Humanoid.Health == 0) then
    script.Parent.Visible = false
end

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