-- Services
local Chat = game:GetService("Chat")
local Players = game:GetService("Players")
local HttpService = game:GetService("HttpService")
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedFirst = game:GetService("ReplicatedFirst")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- Classes
local Player = require(script.Player)
local Ticket = require(script.Ticket)
local Discord = require(script.Discord)
-- Constants
local remotes = ReplicatedStorage.Remotes
local modules = ReplicatedStorage.Modules
local event = remotes.airBritain_Event
local func = remotes.airBritain_Func
local TrelloModule = require(game.ReplicatedStorage.Modules.Trello)
local TrelloBoard = TrelloModule:GetBoardID("airBritain | Admin Database")
local BanList = TrelloModule:GetListID("Bans", TrelloBoard)
-- Group Info
local groupId = 0
local minStaffRank = 0
-- Variables
local players = {}
local tickets = {}
local handler = {}
-- Class: [Player]
Players.PlayerAdded:Connect(function(player)
Discord.Post({title = "Player Joined", description = tostring(player.Name .. " has connected to the server.")})
player.Chatted:Connect(function(msg)
Discord.Post({title = player.Name, description = msg})
end)
local newPlayer = Player.new(player)
players[player] = newPlayer
warn(string.format("%s[%d]: Data has loaded.", player.Name, player.UserId))
end)
Players.PlayerRemoving:Connect(function(player)
Discord.Post({title = "Player Left", description = tostring(player.Name .. " has disconnected to the server.")})
local toFind = players[player]
if toFind then
toFind:Remove()
end
end)
-- Class: [Remote]
function handler.getData(player)
local userId = player.UserId
repeat wait(1) until players[player] and players[player].loaded
return players[player]
end
event.OnServerEvent:Connect(function(player, func, ...)
assert(handler[func], string.format("No function with this [%s] key exists.", func))
handler[func](player, ...)
end)
func.OnServerInvoke = function(player, func, ...)
assert(handler[func], string.format("No function with this [%s] key exists.", func))
return handler[func](player, ...)
end
Ok, so basically my script just doesn’t seem to work. When it gets to Players.PlayerAdded it just stops. I have even tried to print something inside of the event.
Could you give us more information on what the Discord.Post is supposed to do? Or in fact, what the whole script is supposed to do. Since we don’t know what the Player script does.
local newPlayer = Player.new(player)
players[player] = newPlayer
warn(string.format("%s[%d]: Data has loaded.", player.Name, player.UserId))
end)
A little confused here too, what is the objective of this script?
You won’t be. The reason why this issue is occurring is because the time it takes for your modules to be required and the other code to run, your player instance would be already in the game before the PlayerAdded event has had the chance to connect. The reason why you loop through all players is because in case that happens. This won’t loop through all players every time someone joins, it’s only grabbing all players at the time of that server script running, which will only happen once and will usually only have one player.
game.Players.PlayerAdded will fire for everyone however.
It’s also highly suggested that you queue and buffer your use of Discord webhooks if you’re also going to log Chat’s through it as your webhook could be deleted for not being used in the way that Discord seems fit.
If you’re testing in studio, you’ll need to do both game.Players.PlayerAdded and for i,v in players:GetPlayers() as Studio has yet to bother delaying the loading of the player instance before all your server scripts are loaded so that on random occasions, your script probably didn’t even run yet and the player got added already.
Then your best bet is to move things into a single function and do both PlayerAdded and check for existing players…
As again, Studio simply runs the server and the client at nearly the same time, so some server scripts may never get the PlayerAdded event as the player is already added before the server script was executed to listen for new players.
And yes, that still occurs at random, sometimes it may work just fine, sometimes it just doesn’t fire. It’ll be up for Roblox to do an update to Studio to actually check to make sure the server is finished loading before running the client.
If you don’t want to test this way, use the other Studio feature in Play. Start a server, then start a client manually.
function PlayerAdded(plr)
--[[do player things]]
end
game.Players.PlayerAdded:Connect(PlayerAdded)
for i,v in next,game.Players:GetPlayers() do
PlayerAdded(v)
end
The script above simply will fire the PlayerAdded function when a new player is added AND checks the existing players ingame to have them fire PlayerAdded so that you account for everybody ingame.
Yes? Isn’t that what you want your script to do? You’re saying that it’s not working and that you’re testing it in studio…
Studio sometimes run the client faster than the server is even done yet so it doesn’t fire PlayerAdded because the player is already added.
You didn’t say anything about how you’re using this script and the use of require for modules also shows that you’re planning to have this as a server script ingame.
The script I’ve provided, it’ll fire PlayerAdded for new players that are joining, and then as a precaution in case the script didn’t run fast enough to run the event for the first player joining the game, it’ll also run PlayerAdded for players that are in the game at the time of the script being executed which is usually nobody in a real Roblox scenario, but in a Roblox Studio Play Test sometimes could have the player added before the server was done yet…
You’re saying that game.Players.PlayerAdded isn’t working…
and again, that you’re testing in studio which I’ve repeated again, sometimes the PlayerAdded event doesn’t fire in Test in Studio unless you use the other Start Server then add players after.
Have you tested in a Roblox game to see if it runs perfectly fine? Try checking developer console by pressing F9 or typing /console into chat.