Game pauses/freezes for a short period of time when I play it

So when I start the game in Studio, there’s a delay before the game starts, and in realtime, the game just freezes. There are some things I think could be the issue:

Most scripts start with ;

wait()
local player = game.Players.LocalPlayer
local char = workspace:WaitForChild(player.Name)
local Humanoid = char:WaitForChild(“Humanoid”)
local HRP = char:WaitForChild(“HumanoidRootPart”)
local T = char:WaitForChild(“Torso”)

I think this could the issue.

2 Likes

Is the wait() very crucial for your code? Try removing it and testing the game.

1 Like

Still has the issue, I’m really thinking the WaitForChild is the core problem.

If this is about scripting, then it belongs in #development-support:scripting-support. You will be able to get more help there. You should be able to move it in the settings under the topic.

Moved to scripting support. #30characters

Are you talking about lag, or is the script that you have created pausing? It should not be because of WaitForChild().

You should never have a wait at the beginning of your script, regardless of what it is. This is a code smell which is a programming anti-pattern. Use yielding functions and an event-driven code base to work through your issues. Other than that, your code has a few noticeable issues practice wise:

  • Are you adding a wait because of the LocalPlayer index? LocalPlayer implicitly exists for LocalScripts. Any kind of yield is unnecessary.

  • Don’t find the character by searching the Workspace for an instance named a player. It’s not canonically correct and it can produce edge cases for conflicting names.

  • Use clear variable names for the sake of readability and maintainability. Try to avoid one-letter variable names as well as short and undescriptive names.

  • GetService is the canonical method to retrieve services.

local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
-- Rest of your WaitForChilds here: use Humanoid, RootPart and Torso
4 Likes

I believe wait at the top of scripts is an artifact of when certain edge cases existed in some ancient old pre-2015 game code (not sure what, but I remember it being in CoreScripts back in ye olde’ days)

1 Like

does this pause happen on the server too?

Still have it, what if I did a module script that has;

local player = game:GetService(“Players”).LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

in it?

Because I have 12 scripts that uses this. That might be part of the problem

Yes, and its worse in the server.

This probably isn’t the entirety of your code or a good enough portion. Can you provide more information? Is this in a loop? Just because you think it’s the issue doesn’t mean it is.

https://gyazo.com/ca0b09d0e97256d55bc172a56ba03d1a

These are the scripts I assume are the issue where the issue lies, when I remove them. The pause went away

That is a lot of localscripts, but I don’t think those are the issue unless they are all running at once. I was expecting actual code however. This complicates things. You’re going to need to go through each and tell me what they’re each doing.

Edit: Where did you grab this code from?

These scripts work with half like “Boost, Stomp, and or Slide” as movesets.

And others for when the player interacts with something