Script Runs Twice in Multi Place Game?

Yesterday I had a weird problem in a multi-place game. Now, I am having another weird issue. In the starter place, everything works fine, but, as I move things to sub places, it starts to break. Now, some of my scripts are running twice and it only happens occasionally. As you can see, the GUI here has duplicate teleports

I could easily fix this problem by enabling clip descendants, but, I want a real fix for this and I want to know why some scripts run two times. More information provided in my reply below v

1 Like

Short answer: No! scripts do not run twice under any instance! The only reason you would ever see anything in your code run twice is because you explicitly told it to. You are probably dealing with a race condition (where two threads produce different outcomes if one is finished before the other)

For this example, we barely have any information from this thread to help but I’ll still throw in my thoughts. it looks like you are generating a list of places to teleport to but it’s generating the list twice. Here’s a few questions you could ask yourself then:

  • Is the code for the teleport UI getting ran twice?
  • Are you trying to cover for when events happen before functions are connected to them?
  • Is this happening when you firstly open the UI up or are you making the list each time you open the UI?

These question could help you infer where the problem may be. If you need more help you will have to provide more details. Code is much appreciated.

1 Like

I’ll provide more information from another script that is also running twice. On-join this script runs twice when it is in starter character scripts. This has also never happened in the starter place, but it has only started happening in the sub-place. Here is the script and the hierarchy:

image

Server script:
image

Client script:
image

I also added print(script.Parent:GetFullName()) to both the server and the client script and as you can see it printed it twice from both the client and server script: image

Upon inspection of the output, no your code on the server does not run twice. It seems that your client does though and here’s why:

The method you’re using to deliver code (via starter character) is not good for the practice you have. These scripts run as soon as each character spawns, so each time you are connecting another function to re.OnClientEvent. Connections do not go away once the script gets destroyed! Centralize your code under StarterPlayerScripts, connect the function once, and your problem may be solved.

That is definitely a problem, that’s true, but if I remove the local script entirely, the server script still runs twice. There is also one player that has spawned so the server script should only run once. So what I’ve changed right now to test is I’ve commented everything except for the print, I have taken the server script out of the folder and I have removed the local script. These are the visuals image image image

I just found out that every single script in the startercharacter and StarterGui runs twice, but in the starter place, it runs once, like it should.

Then 2 characters are being spawned. Your code works fine but the methodology in running the code is bad as I said earlier. Make a Script in ServerScriptService that does this and listen for when players and their characters get added.

Ohhh lmao I forgot to uncheck CharacterAutoLoads so the character was being added twice each time. Thanks