#Explanation assistance/Understanding

1 messages · Page 1 of 1 (latest)

tiny umbra
#

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

#

BASICALLY, is 'while humanoid' necessary?, and is defining humanoid more efficient for writing this script, that being the reason it's there? otherwise i cant think of why roblox decided to define it.

jolly flame
#

On the server side of things, you will likely never ever need to wait for anything

#

On client due to replication and many other factors waiting is safe to do

tiny umbra
#

i should probably get through some more basic scripting tutorials since I haven't really made code specific to client or server yet, or learned about that

#

sorry for probably just wasting ur time for that regard

jolly flame
#

Client is like a outside observer

tiny umbra
#

okay thank you

jolly flame