PlayerAdded suddenly not working?

Hello everyone,

I was working on my game fine until 30 mins ago when I decided to play test it and for some reason the Players.PlayerAdded aren’t firing. I tried it on Studio and also in Roblox Player but it doesn’t seems to work, it was working perfectly fine until then.

Is this a bug or I’m doing something wrong?

Here’s the code:

local overhead = require(game.ServerStorage.Framework.Overhead)
local ai = require(game.ServerStorage.Framework.AI)
local framework = require(game.ServerStorage.Framework.Main)
local dataServ = game:GetService("DataStoreService")
local store = dataServ:GetDataStore("SaveStuff")
local cache = {}
local infoOfPCS = {
	['SimplePC'] = 50,
	['AdvancedPC'] = 500,
	['SuperPC'] = 2000,
	['NASAPC'] = 10000
}

game.Players.PlayerAdded:Connect(function(player)
	local ls = Instance.new("Folder", player)
	ls.Name = 'leaderstats'
	local money = Instance.new("IntValue", ls)
	money.Name = "Money"
	money.Value = 50
	local hackInfo = Instance.new("IntValue", ls)
	hackInfo.Name = "Information"
	local plot = Instance.new("ObjectValue", player)
	plot.Name = 'PlayerPlot'
	player.CharacterAdded:Connect(function(char)
		repeat wait() until char.Head
		overhead.Add(player, player.Character)
		framework.replaceScripts(player)
		print('Added PC scripts to '..player.Name)
	end)
end)


game.Players.PlayerRemoving:Connect(function(player)
	local money = player.leaderstats.Money.Value
	store:SetAsync(player.UserId.."_money", money)
	framework.resetCache(player)
	print("Reset "..player.Name.."'s cache.")
end)

game.ReplicatedStorage.Events.Manage.OnServerEvent:Connect(function(player, ins, mode)
	local plot = player.PlayerPlot.Value
	if mode == "FireEmployee" then
		ai.NPC.Fire(ins, plot)
	elseif mode == "HireEmployee" then
		local plrMoney = player.leaderstats.Money
		if plrMoney.Value >= 200 then
			plrMoney.Value -= 200
			local employee = game.ServerStorage.Employees['Employee'..math.random(1,2)]:Clone()
			employee.Parent = plot.Employees
			employee.Name = "Employee"
			employee.Owner.Value = player
			employee:MoveTo(plot.Base.Position)
			billboard.Add("-200 Money", employee, 5, Color3.new(1, 0, 0))
		else
			billboard.Add("You need "..(200-plrMoney.Value).." more to hire an employee.", player.Character, 5, Color3.new(1, 0, 0))
		end
	elseif mode == "DestroyPC" then
		
	end
end)

game.ReplicatedStorage.Events.PlaceItem.OnServerEvent:Connect(function(player, item, slot)
	local plrMoney = player.leaderstats.Money
	if plrMoney.Value >= infoOfPCS[item] then
		plrMoney.Value -= infoOfPCS[item]
		local model = game.ServerStorage.Computers:FindFirstChild(item):Clone()
		model.Parent = slot.Parent
		model:SetPrimaryPartCFrame(slot.Part.CFrame)
		model.MovePart:Destroy()
		slot:Destroy()
		model.Owner.Value = player
		model.Screen.EmployeeHackingHandler.Controller.ReportTo.Value = model
		model.Screen.EmployeeHackingHandler.Controller.Parent = player.PlayerGui.PCScripts
	else
		billboard.Add("You don't have enough money.", player.Character, 5, Color3.new(1,0,0))
	end
end)

ai.Init()
framework.Init()

--[[for i, model in pairs(workspace:GetChildren()) do
		if model.Name == "Computer" or model.Name == "Prisoner" then
			if model.Owner.Value == player then
				model:Destroy()
			end
		end
	end]]
1 Like

Can we see the full code? Maybe that will help.

Sure, I added it to the original post

It could be the modules you required at the top of the script. To fix this, you could define the function of the event. Then connect the event to the function. Just to be safe, do Players:GetPlayers() and loop through the array after connecting the function in case a player joined before the event was connected.

local function PlayerAdded(player)
    -- code
end

game.Players.PlayerAdded:Connect(PlayerAdded)
for _, player in pairs(game.Players:GetPlayers()) do
    coroutine.wrap(PlayerAdded)(player)
end

I fixed, turns out it was 2 modules requiring each other.