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

01local Admin = require(script.DollorsAdmin)
02script.DollorsAdmin.Parent = game.ReplicatedStorage
03 
04local Settings = {
05    Prefix = '?', --The key you want to use before a command EX: ?kill [user]
06    OwnerOnly = false, --If you want only the owner to use commands
07}
08 
09Admin.GetPrefix(Settings.Prefix)
10 
11if Admin.Loaded == true then
12    print('Admin Loaded!')
13else
14    warn('Admin Could not load. Please rejoin the server.')
15end
View all 44 lines...

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 — 5y

1 answer

Log in to vote
2
Answered by
Ankur_007 290 Moderation Voter
5 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:

01local setupCompleted = false
02local function Setup() -- Since this hasn't been called yet, it doesn't yield and affect the PlayerAdded:Connection
03   -- Do whatever you need
04    setupCompleted = true
05end
06 
07game.Players.PlayerAdded:Connect(function()
08    if not setupCompleted then
09        Setup()
10        repeat wait() until setupCompleted
11    end
12    -- Now you can be sure that everything is setup
13end)
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 — 5y
Ad

Answer this question