I’m looking at two different excerpts of code from the Roblox ‘Core’ and ‘Gameplay Scripting’ tutorials, in which the two use different ways of fetching the Humanoid object of Players for their respective reasons.
One uses task.spawn() while the other doesn’t. But why?
The following is from the Create Player Hazards section of Core tutorial:
local function onPlayerAdded(player)
-- Reset player coins to 0
updatePlayerCoins(player, function(_)
return 0
end)
player.CharacterAdded:Connect(function(character)
-- WaitForChild would stop the player loop, so below should be done in a separate thread
task.spawn(function()
-- When a player dies
character:WaitForChild("Humanoid").Died:Connect(function()
-- Reset player coins to 0
updatePlayerCoins(player, function(_)
return 0
end)
end)
end)
end)
end
-- Initialize any players added before connecting to PlayerAdded event
for _, player in Players:GetPlayers() do
onPlayerAdded(player)
end
And the following is from a script of the Gameplay Scripting tutorial:
local Players = game:GetService("Players")
local setupHumanoidAsync = require(script.setupHumanoidAsync)
local function onCharacterAdded(player: Player, character: Model)
local humanoid = character:WaitForChild("Humanoid")
setupHumanoidAsync(player, humanoid)
end
local function onPlayerAdded(player: Player)
-- Call onCharacterAdded if the player already has a character
if player.Character then
onCharacterAdded(player, player.Character)
end
-- Call onCharacterAdded for all future character spawns for this player
player.CharacterAdded:Connect(function(character: Model)
onCharacterAdded(player, character)
end)
end
-- Call onPlayerAdded for any players already in the game
for _, player in ipairs(Players:GetPlayers()) do
onPlayerAdded(player)
end
As far I can tell, the only difference between the two is an extra layer of the onCharacterAdded function in the second case, but I don’t see how it correlates to not needing task.spawn() since the original function in the for loop needs the function to return before going to the next iteration, and a bad case of WaitForChild will cause a loop delay without a task.spawn().
This raised some questions about how luau executes code and why task.spawn() is necessary in the first case and not the second.