I have made 2 scripts, one was with a ROBLOX's AI, I am new to scripting, and the AI included defining humanoid as a variable; I didn't understand the necessity for defining humanoid like that though.
Anyways, I figured, if i'm using a characteradded function, it will/there will always BE a humanoid, so there isn't a NEED for defining 'humanoid' but when I rewrote it to work without it, I did realise it technically does make it easier to locate and change the humanoid properties, since as u see in the script I edited without the humanoid variable, I had to add a lot of extra words/characters into the script, so I figured humanoid was defined to make it easier to locate humanoid, though I do not understand the 'while humanoid' part, if it will ALWAYS be a humanoid.
here's both scripts. it's supposed to act like poison, damage the player until ~5 hp in .2 sec ticks, so they dont die.
-- first script (assist from robloxs ai)
local function playerAdded(player)
player.CharacterAdded:Connect(function(character)
local humanoid = character:FindFirstChild("Humanoid")
while humanoid and humanoid.Health > 5 do
humanoid:TakeDamage(5)
print(player.Name .. ' has ' .. math.floor(humanoid.Health) .. ' health.')
task.wait(0.2)
end
end)
end
game.Players.PlayerAdded:Connect(playerAdded)
-- edited script, but without humanoid as a variable.
local function playerAdded(player)
player.CharacterAdded:Connect(function(character)
while character.Humanoid.Health > 5 do
character.Humanoid:TakeDamage(5)
print(player.Name .. ' has ' .. math.floor(character.Humanoid.Health) .. ' health.')
task.wait(0.2)
end
end)
end
game.Players.PlayerAdded:Connect(playerAdded)
Sorry for the unnecessarily long explanation