Humanoid is not a valid member of model?

So I have a random number generator which searches through a number of players which generates their name. For some reason, it keeps giving me the Humanoid is not a valid member of model, even though the player is there. I am not sure why it is doing that. I even tried waitforchild.
My code:

repeat
local randomhammer = random:NextInteger(1, #hammertable)
person = hammertable[randomhammer]
timoutw = timoutw + 1
wait()
until game.Players:FindFirstChild(person) or timoutw > 60
if not game.Players:FindFirstChild(person) then
getranplayer()
person = ranp1
end
local plare = game.Players:FindFirstChild(person)
local timout2 = 0
repeat wait() timout2 = timout2 + 1 until plare.Character or timout2 > 100
kidnapperson = person
if plare.Character then
local humanoid = plare.Character:FindFirstChild("Humanoid")
if humanoid then
game.Workspace.Chair.Seat:Sit(plare.Character.Humanoid)
plare.Character.Humanoid.Health = 0
end
end

Get random player generates a random player and works fine. Any healp is appreciated.

2 Likes
if humanoid then   
  game.Workspace.Chair.Seat:Sit(plare.Character.Humanoid)
  plare.Character.Humanoid.Health = 0
end

Have you tried this?

if humanoid then
  game.Workspace.Chair.Seat:Sit(humanoid)
  humanoid.Health = 0
end
1 Like

The code doesn’t reach that part.

1 Like

well which part does it reach?

The humanoid doesn’t get found so it never reaches the part inside if humanoid then

Use WaitForChild() like this:

local humanoid = plare.Character:WaitForChild("Humanoid")
3 Likes

I did that, but it says infinite yield

Are you sure you’re getting the actual player’s character?

The player isn’t even being found. I tried to get the player with developer console, but it doesn’t work

Try using a print on “plare” to see what it returns to you, if it returns the actual player. Try printing the character to see if it even prints, if it prints the character’s name, then its working, if not, theirs a problem in one of them.

Edit: Try indenting your code so it can be easier to read for us to help you.

On the second line, the “randomhammer” variable, you capitalized Random wrong, as “random”. Change it to

Random:NextInteger(1, #hammertable)

.

random is a variable for Random.new()

Why… There is no roblox api for Random

where is ranp1 coming from, is your person variable an actual playername at that point?

Yes ranp1 is an actual random generated person name.

Is this a local or server script?

It is a server script, yeah…

Could we take a look at how hammertable is being generated and what it contains? I believe the problem might be with that.

just remove all your timeouts so you can see where it actually breaks. you are silently catching errors, its horrible :frowning:

you can add them back when you know your controlled conditions work properly

game.ReplicatedStorage.Hammers.Event:Connect(function(plrtable)
	hammerevent = true
	hammertable = plrtable
	for i,v in pairs(hammertable) do
		print(v)
	end
end)
local amount = 3
local players = {}
function hi(x)
	if amount > 0 then
	amount = amount - 1
	local y = x.Backpack
	local z = game.Lighting["Hammer"] 
	z:Clone().Parent = y  
	table.insert(players, x.Name)
	if amount == 0 then
		game.ReplicatedStorage.Hammers:Fire(players)
	end
	local char = x.Character
	end
end

script.Parent.ClickDetector.MouseClick:Connect(hi)

2 seperate scripts.