#Can someone help answer a few simple questions about functions within a function?

1 messages · Page 1 of 1 (latest)

tawdry snow
#

I have a few simple questions about functions within a function.

For example, this is my current code

function GameInProgress()
    print("The game is in progress")
        local playersingame = game.Players:GetChildren()
        task.spawn(function()
            
            
        end)
        while true do
            if #playersingame >= 2 then
                    print("Round is still in progress")
            elseif #playersingame == 1 then
                print("We have a winner")
            end
            wait(1)
        end
end

Say I want to check to see if a player died so I can take them out of the playersingame table.

If I use

player.Character.Humanoid.Died:Connect(function()

Inisde the function, how will it be called? Would I have to do a while true do loop, or will it be called while inside of a function.

Please feel free to ask for clarification

#
function GameInProgress()
    print("The game is in progress")
        local playersingame = game.Players:GetChildren()
        task.spawn(function()
            
            
        end)
-------------------------------------------------------------
  player.Character.Humanoid.Died:Connect(function()
    print("player died")
end
------------------------------------------------------------------
        while true do
            if #playersingame >= 2 then
                    print("Round is still in progress")
            elseif #playersingame == 1 then
                print("We have a winner")
            end
            wait(1)
        end
end

If I call the GameINProgress function, and 2 mintues later someone dies, will it print "player died" ?

limpid frigate
#

Yes it will. It will even trigger if the round ends.

#

Im not sure how excatly i can explain it better.

Think of .Died:Connect() like setting a trap. If you set the trap (call the function), it’ll go off whenever someone dies even minutes later.
But if you never set the trap, nothing happens when they die.
Once the trap is set (event is connected), it keeps working forever even after the function ends until you Disconnect the events.

tawdry snow
limpid frigate
#

No, i would perfer doing this:

local deathEvent = player.Character.Humanoid.Died:Connect(function()
  print("Player died")
end

-- If round done
deathEvent:Disconnect()
tawdry snow
#

I'm sorry im new to this so im going to have dumb questions.

How would I get define the player in the workspace? If it was in game.Players, I think I would just do

Local Players = game.Players:GetChildren()

Would it be something like

local players = {}
for _, child in pairs(game.Workspace:GetChildren()) do
    if child:FindFirstChild("Humanoid") then
        table.insert(players, child)
    end
end
limpid frigate
#

Thats will works, either game.Players:GetChildren() or game.Players:GetPlayers()