Hello I’m trying to make it so that when a player clicks a certain brick, there is a 50% chance that the person dies, and a 50% chance that they live. Here’s my code
script.Parent.ClickDetector.MouseClick:connect(function(hit)
local randomNumber = math.random(1,2)
if randomNumber == 1 then
local human = hit.Parent:FindFirstChild(“Humanoid”)
human.Health = 0
script.Parent.BrickColor = BrickColor.new(“Bright red”)
elseif randomNumber == 2 then
script.Parent.BrickColor = BrickColor.new(“Bright green”)
end
end)
The second part of the script is working fine, but no matter how many times the player clicks it they won’t die, and the brick will not change red. My error message is “Workspace.Part.Script:5: attempt to index nil with ‘Health’”
when you do “hit”, it is only going through the player. So when you do hit.Parent, it gets players. instead, go through the character and find the humanoid.
The reason why you’re getting this error: “Workspace.Part.Script:5: attempt to index nil with ‘Health’
is because this line: local human = hit.Parent:FindFirstChild(“Humanoid”)
is looking for the Humanoid in the wrong place.
(edit: human is nil because it was not found, and the error says you’re trying to change nil.Health, which isn’t a thing)
According to the ClickDetector.MouseClick docs, the parameter that you named hit is actually the player who clicked. (Not the character in the Workspace, but the Player in the Players.)
So that line of code will look for a humanoid in hit.Parent, which is the Players folder, which doesn’t have any Humanoids in it at all.
@azlentic changed that line to this: hit.Character:BreakJoints()
This works because hit is a Player, and the Player’s property Character points to the character model in Workspace. :BreakJoints then destroys all joints in the character, including the Neck, killing it.