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

Humanoid is not a valid member of model... how do i fix?

So, i'm trying to make this game where you run from someone and you can place walls to slow them off. But, it's suposed to kill the player if they spawn too much walls (5) in the span of 10 seconds. But i get the error on title and i don't know how to fix it. (by the way i'm not that great at scripting and my friend who does know lua helped me a bunch, if that matters) Here's the code. It highlights the 9th line, which also means line 22 won't work.

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local block = game.Workspace:FindFirstChild("Block", true)
local blockCount = 0
local blockTimer = 0

player.CharacterAdded:Connect(function(character)
	character.Humanoid.MaxHealth = 1000
end)

mouse.Button1Down:Connect(function()
	blockCount = blockCount + 1
	local newBlock = block:Clone()
	newBlock.Name = "Block"..blockCount
	newBlock.Parent = game.Workspace
	newBlock.CFrame = player.Character.Head.CFrame - player.Character.Head.CFrame.LookVector

	if blockCount > 5 and tick() - blockTimer >= 10 then
		print(player.Character.Humanoid)
		wait(1)
		player.Character.Humanoid:TakeDamage(1000)
		blockCount = 0
		blockTimer = 0
	end)

	delay(15, function()
		newBlock:Destroy()
		blockCount = blockCount - 1
	end)

	blockTimer = tick()
end)

game:GetService("RunService").Heartbeat:Connect(function()
	if tick() - blockTimer >= 10 then
		blockCount = 0
		blockTimer = 0
	end
end)

print(player)

1 answer

✓ Accepted answer

Hello! The answer is the humanoid has not loaded yet. The character has, but it's still loading the humanoid, body parts, body color, etc.. It's a pretty easy fix though. Using :WaitForChild() you can yield the function (pause it) until it is found. If it has not found it within 5 seconds (which is the default), it will end the function.

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local block = game.Workspace:FindFirstChild("Block", true)
local blockCount = 0
local blockTimer = 0

player.CharacterAdded:Connect(function(character)
    character:WaitForChild("Humanoid",5).MaxHealth = 1000 -- the 5 inside WaitForChild says how long to yield for until cancelling the wait, math.huge will make it yield permanently until it finds it
end)

mouse.Button1Down:Connect(function()
    blockCount = blockCount + 1
    local newBlock = block:Clone()
    newBlock.Name = "Block"..blockCount
    newBlock.Parent = game.Workspace
    newBlock.CFrame = player.Character.Head.CFrame - player.Character.Head.CFrame.LookVector

    if blockCount > 5 and tick() - blockTimer >= 10 then
        print(player.Character:WaitForChild("Humanoid")
        wait(1)
        player.Character:WaitForChild("Humanoid"):TakeDamage(1000)
        blockCount = 0
        blockTimer = 0
    end)

    delay(15, function()
        newBlock:Destroy()
        blockCount = blockCount - 1
    end)

    blockTimer = tick()
end)

game:GetService("RunService").Heartbeat:Connect(function()
    if tick() - blockTimer >= 10 then
        blockCount = 0
        blockTimer = 0
    end
end)

print(player)

Also, you are running this from a LocalScript. If this is a multiplayer game, it wouldn't kill you on everybody else's screen. You can look into PlayerAdded for a pretty easy solution on a normal Script. Also, I dont see the need for setting the health to 1000 only to deal 1000 damage. Setting the health to 0 would be a guaranteed kill regardless of ForceFields or hacks. Hope this helps!

6 comments

0

I recommend not putting the second argument in :WaitForChild(), because after 5 seconds and the child is still nonexistent (or nil) it will return nil.

0

Unlike without the second argument, which will yield forever until the child has been found.

0

Also, you said that the timeOut (or the second argument) default is 5, which is incorrect. You meant that after 5 seconds of the child still nonexistent, it will print a warning, not ending the function and return nil.

0

The timeOut argument is optional, btw.

0

I thought I edited that to feature it... I'll add it now though! Thanks!

0

Had to modify the script a bit cause the parentheses we're a bit messed up but it worked. Thanks.

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