Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Not detecting player added?

Asked by
DollorLua 235 Moderation Voter
4 years ago

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...

0
put a wait() before the function DeceptiveCaster 3761 — 4y

1 answer

Log in to vote
2
Answered by
Ankur_007 290 Moderation Voter
4 years ago

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)
0
Thank you for the answer but now that i've seen this I have made a better version of an admin script and noticed that many yeilding functions such as game.ServerScriptService:GetChatService (I totally forgot the actual layout of it) will ruin the player added function, and editting it I check on the go now and not right away so I get the important part completed first. I accept this as an answer. DollorLua 235 — 4y
Ad

Answer this question