Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why the second function does not activate ?

Asked by 2 years ago

The first function does activate, but the second only when i respawn. This code is not mine, im using it to understand.

function onPlayerEntered(player)
    repeat wait() until player.Character ~= nil
    print(player.Character)
    local scr = script.TrySprintScript:Clone()
    scr.Parent = player.Character
    scr.Disabled = false    
    player.CharacterAdded:Connect(function(char)
        print(char)
        local scr = script.TrySprintScript:Clone()
        scr.Parent = char
        scr.Disabled = false
    end)        
end

0
There is only one function though? johnoscarbhv1 137 — 2y
0
johnoscarbhv1 I believe they're talking about the anonymous function that is Connected to player.CharacterAdded strangejmaster43 139 — 2y

1 answer

Log in to vote
0
Answered by 2 years ago

The second function only runs when the character is added i.e. the actual model of the player in the Workspace. .CharacterAdded only runs once when the character is as said above. You do the repeat wait() until player.Character ~= nil after the player joins the game which takes a second, while that happens the character is being added. Since the code waits for the character to be added once the player.CharacterAdded listener is added the character is already added meaning it's not triggered. Once the player respawns their character is added into the workspace firing connecting the function.

I really hope I could explain this well enough just comment if you have any questions!

0
Thank you! I was thinking about this, but everything runs in the background so it's kind of hard to see. juniorlokipo 2 — 2y
0
No Problem! strangejmaster43 139 — 2y
Ad

Answer this question