I was creating a admin script and I kept having a problem were it wouldn't detect a player being added. I've sent prints through and it works until it checks for the player to be added which is confusing. This has worked for me before but not now.
local Admin = require(script.DollorsAdmin) script.DollorsAdmin.Parent = game.ReplicatedStorage local Settings = { Prefix = '?', --The key you want to use before a command EX: ?kill [user] OwnerOnly = false, --If you want only the owner to use commands } Admin.GetPrefix(Settings.Prefix) if Admin.Loaded == true then print('Admin Loaded!') else warn('Admin Could not load. Please rejoin the server.') end local GameOwnerName local PlaceId = game.PlaceId local PlaceInfo = game:GetService("MarketplaceService"):GetProductInfo(PlaceId) if PlaceInfo.Creator.CreatorType == "Group" then GameOwnerName = game:GetService("GroupService"):GetGroupInfoAsync(PlaceInfo.Creator.CreatorTargetId).Owner.Name elseif PlaceInfo.Creator.CreatorType == "User" then GameOwnerName = PlaceInfo.Creator.Name end local AdminIDs = {} --Must be user ID's. Game owner's ID is automatically put here local OwnerID = game.Players:GetUserIdFromNameAsync(GameOwnerName) table.insert(AdminIDs, #AdminIDs + 1, OwnerID) game.Players.PlayerAdded:Connect(function(plr) print(plr.Name) for i, v in pairs(AdminIDs) do if plr.UserId == v then plr.Chatted:Connect(function(msg) if msg:sub(1, 1) == Settings.Prefix then Admin.SendCommand(msg) end end) end end end)
I don't understand why at the end at line 33 I get no error but it never printed the players name on add...
This probably due to yielding methods like GetGroupAsync and GetUserIdFromNameAsync being present before the Players.PlayerAdded connection. You can either move the whole Players.PlayerAdded connection above the setup part or you could wrap the first part of the script in a coroutine. However, since this information might be pretty important to load before the player, you can use such a structure:
local setupCompleted = false local function Setup() -- Since this hasn't been called yet, it doesn't yield and affect the PlayerAdded:Connection -- Do whatever you need setupCompleted = true end game.Players.PlayerAdded:Connect(function() if not setupCompleted then Setup() repeat wait() until setupCompleted end -- Now you can be sure that everything is setup end)