SurfaceGui menu breaks after resetting

I made a 3D main menu for my game with SurfaceGuis, once issue tho, when i reset the menu breaks as a whole.
I cannot interact with it at all.

Ive tried setting the ResetOnSpawn to false and true both do not fix the issue

My main menu script:

local UserInputService = game:GetService("UserInputService")
local ContextActionService = game:GetService("ContextActionService")
local CamControls = require(script.Parent.CamControls)
local TankRemoteEvents = game.ReplicatedStorage.TankRemoteEvents
local DataStoreRemoteEvents = game.ReplicatedStorage.DataStoreRemoteEvents
local Configs = game.Players.LocalPlayer.PlayerGui.Configuration
local Mouse = game.Players.LocalPlayer:GetMouse()

local UnlockedTanks = {}

DataStoreRemoteEvents.GetPlayerStats:FireServer()
DataStoreRemoteEvents.GetPlayerTanks:FireServer()

DataStoreRemoteEvents.GetPlayerStats.OnClientEvent:Connect(function(PlayerStats)
	local MainMenu = game.Workspace.Other.MainMenu.CamPart.MainMenu.MainMenu
	MainMenu.PlayerInfo.PlayerLevel.Text = "Level: "..PlayerStats.Level
	MainMenu.PlayerInfo.PlayerCash.Text = PlayerStats.Cash.."$"
	MainMenu.PlayerInfo.XP.XPAmount.Text = PlayerStats.XP.."/"..PlayerStats.XPNeeded
	MainMenu.PlayerInfo.XP.Bar.Size = UDim2.fromScale(math.clamp(PlayerStats.XP / PlayerStats.XPNeeded, 0, 1), 1)
end)

repeat task.wait() until Configs.GameLoaded.Value == true

local Player = game.Players.LocalPlayer
local Camera = workspace.CurrentCamera

local MainMenu = game.Workspace.Other.MainMenu.CamPart:WaitForChild("MainMenu").MainMenu
local OtherManu = game.Workspace.Other.MainMenu.CamPart:WaitForChild("OtherMenu").OtherMenu

local TankOptions = game.Workspace.Other.MainMenu.TankSelect:WaitForChild("TankOptions").Options
local TankSelection = game.Workspace.Other.MainMenu.TankSelect:WaitForChild("TankSelection").Selection


MainMenu.PlayButton.TextButton.MouseButton1Click:Connect(function()
	print("hello")
	TankRemoteEvents.SpawnTank:FireServer("Leopard2A7")
end)

MainMenu.TanksButton.TextButton.MouseButton1Click:Connect(function()
	CamControls.TankSelectCam(3)
end)

TankOptions.Back.TextButton.MouseButton1Click:Connect(function()
	CamControls.MainMenuCam(3)
end)

i donot know if this will fix it but try adding the script in starter character scripts so it resets each time the player resets

The bug is probably because you didn’t use “CharacterAdded” on your script. Because CharacterAdded event can be used to reinitialize the menu when the player respawns.

Another solution is probably by using “repeat wait() until Players.LocalPlayer.Character” to ensure the character is fully loaded before attempting to reinitialize the menu.

patched this together rq to test it but it still doesnt fix it

local MenuInit = function()
	MainMenu.PlayButton.TextButton.MouseButton1Click:Connect(function()
		print("hello")
		TankRemoteEvents.SpawnTank:FireServer("Leopard2A7")
	end)

	MainMenu.TanksButton.TextButton.MouseButton1Click:Connect(function()
		CamControls.TankSelectCam(3)
	end)

	TankOptions.Back.TextButton.MouseButton1Click:Connect(function()
		CamControls.MainMenuCam(3)
	end)
end

MenuInit()

game.Players.LocalPlayer.CharacterAdded:Connect(MenuInit)

Hmm. Try to disconnect some old connections maybe? (functions)

well reused a bit of code from elsewhere in my game but still nothing

local function onCharacterAdded(character)
	MainMenu.PlayButton.TextButton.MouseButton1Click:Connect(function()
		print("hello")
		TankRemoteEvents.SpawnTank:FireServer("Leopard2A7")
	end)

	MainMenu.TanksButton.TextButton.MouseButton1Click:Connect(function()
		CamControls.TankSelectCam(3)
	end)

	TankOptions.Back.TextButton.MouseButton1Click:Connect(function()
		CamControls.MainMenuCam(3)
	end)

	local humanoid = character:WaitForChild("Humanoid")
	humanoid.Died:Connect(function()
		test:Disconnect()
	end)
end
test = Player.CharacterAdded:Connect(onCharacterAdded)

if Player.Character then
	onCharacterAdded(Player.Character)
end

Oh wait, I probably found the solution. If it doesn’t work again, I can’t find any solution anymore. Try to use this script (there’s table.insert for the fireServer) :

local UserInputService = game:GetService("UserInputService")
local ContextActionService = game:GetService("ContextActionService")
local CamControls = require(script.Parent.CamControls)
local TankRemoteEvents = game.ReplicatedStorage.TankRemoteEvents
local DataStoreRemoteEvents = game.ReplicatedStorage.DataStoreRemoteEvents
local Configs = game.Players.LocalPlayer.PlayerGui.Configuration
local Mouse = game.Players.LocalPlayer:GetMouse()
local Players = game:GetService("Players")

local Player = Players.LocalPlayer
local connections = {}

local function initializeMainMenu()
    local MainMenu = game.Workspace.Other.MainMenu.CamPart:WaitForChild("MainMenu").MainMenu
    local TankOptions = game.Workspace.Other.MainMenu.TankSelect:WaitForChild("TankOptions").Options

    DataStoreRemoteEvents.GetPlayerStats.OnClientEvent:Connect(function(PlayerStats)
        MainMenu.PlayerInfo.PlayerLevel.Text = "Level: " .. PlayerStats.Level
        MainMenu.PlayerInfo.PlayerCash.Text = PlayerStats.Cash .. "$"
        MainMenu.PlayerInfo.XP.XPAmount.Text = PlayerStats.XP .. "/" .. PlayerStats.XPNeeded
        MainMenu.PlayerInfo.XP.Bar.Size = UDim2.fromScale(math.clamp(PlayerStats.XP / PlayerStats.XPNeeded, 0, 1), 1)
    end)

    table.insert(connections, MainMenu.PlayButton.TextButton.MouseButton1Click:Connect(function()
        print("hello")
        TankRemoteEvents.SpawnTank:FireServer("Leopard2A7")
    end))

    table.insert(connections, MainMenu.TanksButton.TextButton.MouseButton1Click:Connect(function()
        CamControls.TankSelectCam(3)
    end))

    table.insert(connections, TankOptions.Back.TextButton.MouseButton1Click:Connect(function()
        CamControls.MainMenuCam(3)
    end))
end

local function onCharacterAdded(character)
    character:WaitForChild("Humanoid")

    for _, connection in ipairs(connections) do
        connection:Disconnect()
    end
    connections = {}

    initializeMainMenu()

    character.Humanoid.Died:Connect(function()
        for _, connection in ipairs(connections) do
            connection:Disconnect()
        end
        connections = {}
    end)
end

Player.CharacterAdded:Connect(onCharacterAdded)

if Player.Character then
    onCharacterAdded(Player.Character)
else
    initializeMainMenu()
end`
1 Like

this works, imma clean it a bit tho.
Thx for helping

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.