Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why the second function does not activate ?

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

2 comments

0

There is only one function though?

0

johnoscarbhv1 I believe they're talking about the anonymous function that is Connected to player.CharacterAdded

1 answer

✓ Accepted answer

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!

2 comments

0

Thank you! I was thinking about this, but everything runs in the background so it's kind of hard to see.

0

No Problem!

This discussion is preserved as it was. New answers, comments, and votes are closed.