Crashes regarding a certain global table script

Hello developers! The following script that I have attached is the reason why my game in mobile crashes (not in pc). When I disabled it, the game worked smoothly in phone, however on enabling it, the client on phone freezes. Please help, I cannot identify the issue.

_G.UserAccessories = {}

local DataStoreService = game:GetService("DataStoreService")
local accessoryData = DataStoreService:GetDataStore("accessoryData")

local Abilities = require(game.ReplicatedStorage:WaitForChild("Modules"):WaitForChild("Abilities"))

game.Players.PlayerAdded:Connect(function(player)
	local data
	local success,errmsg = pcall(function()
		data = accessoryData:GetAsync(player.UserId.."-acc")
	end)
	
	if data then
		print(data)
		_G.UserAccessories[player.UserId] = data
	else
		_G.UserAccessories[player.UserId] = {
			["Accessory"] = {},
			["Face"] = {},
			["Ability"] = {}
		}
	end
	
	for index,v in pairs(_G.UserAccessories[player.UserId]['Ability']) do
		game.ReplicatedStorage:WaitForChild("Remotes"):WaitForChild("AbilityEquipHandle"):FireClient(player,v,"Success",index)
	end 
end)

game.Players.PlayerRemoving:Connect(function(player)
	local currentUserData = _G.UserAccessories[player.UserId]
	print(currentUserData)
	local success,errmsg = pcall(function()
		accessoryData:SetAsync(player.UserId.."-acc",currentUserData)
	end)
	
	if success then
		print("Accessory Data saved.")
	end
end)

game.ReplicatedStorage:WaitForChild("Remotes"):WaitForChild("GetUserAccessories").OnServerInvoke = function(player)
	return _G.UserAccessories[player.UserId]
end

--[[game.ReplicatedStorage:WaitForChild("Remotes"):WaitForChild("ConfirmAbility").OnServerInvoke = function(player,data)
	if player.Abilities:FindFirstChild(data) then
		return true
	else
		return false
	end
end--]]

Thank you for the help! With your solution, please provide an explanation too, I need to know the reason!

1 Like

Does the issue occur if instead of using _G, you use a ModuleScript?


@thatdevmikey Try the following steps:

  1. Create a ModuleScript named UserAccessories, and place it inside of the Modules Folder in ReplicatedStorage
  2. This will need to be the code for the module:
local UserAccessories = {}

return UserAccessories
  1. Replace your server Script’s code with this:
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local modules = ReplicatedStorage.Modules
local Abilities = require(modules.Abilities)
local UserAccessories = require(modules.UserAccessories)

local remotes = ReplicatedStorage.Remotes
local abilityEquipHandle = remotes.AbilityEquipHandle
local getUserAccessories = remotes.GetUserAccessories

local accessoryData = DataStoreService:GetDataStore("accessoryData")

Players.PlayerAdded:Connect(function(player)
	local success, data = pcall(accessoryData.GetAsync, accessoryData, player.UserId.."-acc")

	if success then
		UserAccessories[player.UserId] = if data then data else {
			Ability = {},
			Accessory = {},
			Face = {}
		}

		for index, value in UserAccessories[player.UserId].Ability do
			abilityEquipHandle:FireClient(player, value, "Success", index)
		end
	else
		warn(`Failed to retrieve data for player {player.UserId} ({player.DisplayName})`)
	end
end)

Players.PlayerRemoving:Connect(function(player)
	if UserAccessories[player.UserId] then
		local success, error = pcall(
			accessoryData.SetAsync,
			accessoryData,
			player.UserId.."-acc",
			UserAccessories[player.UserId]
		)

		if success then return end

		warn(`Failed to save data for player {player.UserId} ({player.DisplayName})`)
	end
end)

getUserAccessories.OnServerInvoke = function(player)
	return UserAccessories[player.UserId]
end

Hopefully using a module instead of _G fixes your issue :slight_smile::+1:

Please note: If you are to make any changes to the script I provided, do be careful, as it may introduce issues that weren’t present during testing :sweat_smile:

1 Like

Hey, thank you so much for the response. Your solution fixed the problem! The crashing in mobile is fixed, however, there is still an initial freeze for some time on portrait before the game turns into landscape and functions well. I think its an issue on my end, I’ll see about that too. Any recommendations there?

2 Likes

The freezing might be happening if your game contains many GUIs that are always visible, which will all need to be updated whenever a mobile device switches from portrait mode to landscape. If your desire is for the game to be played in landscape mode only, then an easy way to fix this problem is to set the PlayerGui property ScreenOrientation to one of the landscape enums (LocalScript in ReplicatedFirst):

local Players = game:GetService("Players")

Players.LocalPlayer:WaitForChild("PlayerGui").ScreenOrientation = -- Either: Enum.ScreenOrientation.LandscapeLeft, Enum.ScreenOrientation.LandscapeRight, Enum.ScreenOrientation.LandscapeSensor

Otherwise, try finding ways to minimize the quantity of GUIs being shown at a given time

2 Likes

Thank you so much ! That is infact correct, and now the game works smoothly. All thanks to you :slight_smile:

1 Like