Apparently, a variable for the player name is better than using .Name each time.

  • Here is the script I used:

    local players = game:GetService("Players")
    local debounce = true
    	if debounce then
    		if hit.Parent:FindFirstChild("Humanoid") then
    			debounce = false
    			local plr = players:GetPlayerFromCharacter(hit.Parent)
    			local timeBefore = tick()
    			for i = 1, 1000 do
    			local timeAfter = tick() - timeBefore
    			local timeBeforeTwo = tick()
    			local plrName = plr.Name
    			for i = 1, 1000 do
    			local timeAfterTwo = tick() - timeBeforeTwo
    			print("without variable is: "..timeAfter)
    			print("with variable is: "..timeAfterTwo)
    			debounce = true

    Here is are the results:

    without variable is: 0.084712982177734
    with variable is: 0.063939332962036
    without variable is: 0.084455728530884
    with variable is: 0.062870502471924
    without variable is: 0.085661888122559
    with variable is: 0.062398672103882
    without variable is: 0.079595565795898
    with variable is: 0.06275749206543
    without variable is: 0.083762168884277
    with variable is: 0.063457012176514
    without variable is: 0.080082178115845
    with variable is: 0.06156849861145
    without variable is: 0.072998523712158
    with variable is: 0.063893556594849
    without variable is: 0.077476978302002
    with variable is: 0.062778949737549
    without variable is: 0.083955049514771
    with variable is: 0.062814712524414
    without variable is: 0.082892179489136
    with variable is: 0.062796592712402

    Here are the averages:

    Without the variable resulted in an average of: 0.0815593242645264
    With the variable resulted in an average of: 0.062927532196045

    You can round them if you like. Here is a link: to the reason I did this test. I also did this in order to show how a test should be done in light of the recent discussion: Hope you guys find this interesting. If you have any comments please post them. I would love to know what you think. Have a great day scripting!

    Quick Edit/Note: My computer has an i7 core and the test was performed in studio play mode. Who knows what the results might be on a slower computer and on an actual server.

  • Global Moderator

    Yeah I definitely believe that. Using .Name technically has to reference the Player object's Name which technically will take more time than just saving the name as a variable. Though it's less than 2 hundredths of a second

  • Told you variable is better

  • Global Moderator

    @Phlegethon5778 Yes of course that's faster. But really, it shouldn't make much of a difference in practice. Use whichever makes your code clearer.

  • @Link150 I really did this mainly to see whether or not the people who criticized me for using a variable for plrName were correct in doing so. It may only be a minuscule amount more efficient but I feel better about using the variable now.

  • Nah, I'll still use .Name xD

  • @Phlegethon5778 the downside to using variables is they don't update if the property changes. this is one of those common mistakes where they put their variable as like script.Parent.Text and expect it to change when they're just reassigning the variable. like local text = script.Parent.Text; text = "jejdksjfjfj" that doesn't change the text. but the chances of someone changing their name whilst in a game are pretty low, but you should still compensate for that. there are some that wouldn't require direct accessing, like read-only properties.

  • The times when I use the variable last a very short amount of time and are set when events fire. That means that it will update. I will keep in mind your comments though.

  • Global Moderator

    @incapaz I don't think a player's username can change in real time. I'm sure they would need to close the game and rejoin for the changes to take place.

  • Oh that's helpful to know. Thanks for pointing that out link!

Log in to reply

Looks like your connection to Scripting Helpers was lost, please wait while we try to reconnect.