LocalScript is running twice?

Hello developers!
I am working on a placement system at the moment… and I am having issues with the main Local Script running twice.

I’ve added a print(script.Name) at the top of the script, and as you can see it prints twice despite only one script of it’s name in the game:
image

image

I am confident that this is not an issue with the client code as I inserted a one line script with print("test script") and it produced a similar result:
image

Here’s my server side code:

local Players = game:GetService("Players")

local OriginParts = workspace:WaitForChild("OriginParts"):GetChildren()
local GridsFolder = workspace:WaitForChild("Grids")
local PlayerSpawns = workspace:WaitForChild("PlayerSpawns"):GetChildren()
local GridCreator = require(script.CreateGrid)

local AssignedGrids = {}

for _, Part in pairs(OriginParts) do
	GridCreator(50, Part.Position, Part.Name)
end

local function PlayerAdded(Player)
	local AssignedGridInstance = Instance.new("IntValue", Player) 
	AssignedGridInstance.Name = "Grid"
	while true do
		local Grid = math.random(1,#OriginParts)
		if not AssignedGrids[Grid] then
			AssignedGrids[Grid] = Player
			AssignedGridInstance.Value = Grid
			Player.RespawnLocation = PlayerSpawns[tostring(Grid)]
			pcall(function()Player:LoadCharacter(true)end)
			break
		end
		task.wait()
	end
end

local function PlayerRemoving(Player)
	for Grid, AssignedPlayer in pairs(AssignedGrids) do
		if AssignedPlayer == Player then
			AssignedGrids[Grid] = nil
			break
		end
	end
end

Players.PlayerAdded:Connect(PlayerAdded)
Players.PlayerRemoving:Connect(PlayerRemoving)

When I disable the server script, the issue does not occur anymore. I think that this is an issue with LoadCharacter because after removing that everything works fine.

Thanks for your help!

I think the reason is the call to LoadCharacter. According to the docs this destroys the current character for the player and recreates it. So I suspect your script is running once when the player is initially added and it’s PlayerGui is initialised, then when you call LoadCharacter PlayerGui is being re-created for the new character.

Do you think that killing the player via the humanoid would work better?

If this is a local script which should only execute when the player first joins then move it to the StarterPlayerScripts folder.

Are you killing the player just to get them to respawn somewhere else?

If so can you not just move them to the new location rather than kill them and rely on respawning?

It’s a placement system intergrated with UI, so it doesn’t really belong in StarterPlayer.

I suppose this is a fix, but what about if the player dies on their own?

You can still set the respawn location as you do, then if they die they will respawn at that location. Similar to the way you would implement checkpoints.

1 Like

It has something to do with respawning after loading in, to fix this change the GUI property called ResetPlayerGuiOnSpawn. This should disable it from running twice.
Else there are 2 other alternatives.

  1. Do not let anyone auto-respawn and make a custom auto-respawn script.
  2. Do not put the GUIs inside StarterGui, instead make a custom StarterGui folder, like in ServerStorage.

Though my preference would be disabling ResetPlayerGuiOnSpawn, as this should just fix it.
The best alternative solution would be 2 of the alternatives, if the player does not automatically load when they join the game, though one issue with this is that the PlayerGui always loads even when the player’s character has not spawned.

Another way around this is checking for the Player’s Character before running the script.

local LocalPlayer = game:GetService("Players").LocalPlayer

if not LocalPlayer.Character then script.Parent.Parent:Destroy() end -- destroy the GUI
2 Likes

I ended up with going with the ResetPlayerGuiOnSpawn, although in reality I want the GUI to reset if a player does. I’ll find a workaround eventually. :3