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:
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:
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.
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.
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.
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.
Do not let anyone auto-respawn and make a custom auto-respawn script.
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
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