PlayerAdded function not working

I am trying to make a custom admin command script, but for some reason when I try to run the PlayerAdded function, it just doesn’t work. I have tried to move stuff around, but it still doesn’t work.

There are prints in the code just to see where stops working at, it stops working at print(“Test2”)
The code is as follows:

print("Test1")
local Datastore = game:GetService("DataStoreService")
local AdminData = Datastore:GetDataStore("TH3Administrators")
local AdminKey = "TH3AdminKey012023"
local CurrentAdmins = AdminData:GetAsync(AdminKey)

print("Test2")

game.Players.PlayerAdded:Connect(function(plr)
	print(plr.Name.." has joined the game!")

	local Configuration = require(script.Parent)
	if not game.ReplicatedStorage:FindFirstChild("TH3Events") then
		local NewFolder = Instance.new("Folder")
		NewFolder.Name = "TH3Events"
		NewFolder.Parent = game.ReplicatedStorage
		script.SendNotification.Parent = NewFolder
		script.ServerToClient.Parent = NewFolder
	end

	plr.Chatted:Connect(function(msg)
		local SplitCommand = string.split(msg, " ")
		if SplitCommand[1] == Configuration["Command Configuration"].Prefix.."notify" then
			print("Yippee!")
			local TargetPlayer = SplitCommand[2]
			local Title = SplitCommand[3]
			local Desc = string.sub(msg, #SplitCommand[1]+1+#SplitCommand[2]+1+#SplitCommand[3]+1, #msg)

			local success, result = pcall(game.Players.GetUserIdFromNameAsync, game.Players, TargetPlayer)
			if success then
				TargetPlayer = result
			else
				if not plr.PlayerGui:FindFirstChild(script.ClientHandler.Name) then
					local Clone = script.ClientHandler:Clone()
					Clone.Parent = plr.PlayerGui
				end
				game.ReplicatedStorage.TH3Events.SendNotification:FireClient(plr, "ERROR", "Cannot fetch username from command!")
				return
			end

			local useCommandModule = script.Parent:FindFirstChild("UseCommand")
			if useCommandModule then
				require(useCommandModule).Notify(TargetPlayer, Title, Desc)
			else
				print("UseCommand module not found!")
			end
		end
	end)
end)

Hi there, refer to this reply here: CharacterAdded functions not loading correctly - Help and Feedback / Scripting Support - Developer Forum | Roblox

In a nutshell, your player already exists before you connect to PlayerAdded. You need to run your function for any existing players that have spawned so that they can get caught in the loop of things as well.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.