PlayerAdded event not working?

Hello, I’m having a problem with this script.

local gravitymodule = require(game.ServerStorage.GravityModule)
local space = game.Workspace.Space
local _space = {}

for _, v in pairs(space:GetChildren()) do
	table.insert(_space, v)
end

for _, v in pairs(_space) do
	for _, w in pairs(_space) do
		if v ~= w then
			if v:FindFirstChild("Humanoid") then
				gravitymodule.addForce(v.LowerTorso, w)
			else
				gravitymodule.addForce(v, w)
			end
		end
	end
	table.remove(_space, table.find(_space, v))
end

function addInstance(x)
	local _space = {}
	for _, v in pairs(space:GetChildren()) do
		table.insert(_space, v)
	end
	
	for _, v in pairs(_space) do
		if v ~= x then
			gravitymodule.addForce(x, v)
		end
	end
end

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAppearanceLoaded:Connect(function(char)
		char.Animate.idle:Destroy()
		char.Parent = game.Workspace.Space
		char.Humanoid:ChangeState(Enum.HumanoidStateType.Physics)
		
		local _space = {}
		for _, v in pairs(space:GetChildren()) do
			table.insert(_space, v)
		end
		
		for _, v in pairs(_space) do
			if v ~= char then
				gravitymodule.addForce(char.LowerTorso, v)
			end
		end
		
		print("asdf")
	end)
end)

Everything runs fine until the PlayerAdded event. Nothing after than runs. This script is in ServerScriptService.

It’s supposed to simulate gravity, using lineforces. The last part is supposed to check when a new player joins to add it’s character and simulate gravity for IT also. It doesn’t even print "asdf".

The player might be joining before you connect to PlayerAdded (mainly studio)

A solution to this would be to make a PlayerAdded function.

We then connect Players.PlayerAdded to this new function and we loop through all currently connected users and fire PlayerAdded.

I also now check inside of PlayerAdded incase the Appearance had loaded before we listened onto CharacterAppearanceLoaded

local function PlayerAdded(plr)
	local function AppearanceLoaded(char)
		char.Animate.idle:Destroy()
		char.Parent = game.Workspace.Space
		char.Humanoid:ChangeState(Enum.HumanoidStateType.Physics)
		
		local _space = {}
		for _, v in pairs(space:GetChildren()) do
			table.insert(_space, v)
		end
		
		for _, v in pairs(_space) do
			if v ~= char then
				gravitymodule.addForce(char.LowerTorso, v)
			end
		end
		
		print("asdf")
	end

	plr.CharacterAppearanceLoaded:Connect(AppearanceLoaded)

	if plr:HasAppearanceLoaded() then
		AppearanceLoaded(plr.Character)
	end
end

game.Players.PlayerAdded:Connect(PlayerAdded)

for _,Player in ipairs(game.Players:GetPlayers()) do
	task.spawn(PlayerAdded, Player)
end

Or could I just use wait or a repeat until it exists loop?

I don’t understand, could you explain more?

Could I just use something like this:

repeat
wait()
until game.Players.PlayerAdded ~= nil

or would that not work…

This is also weird because we always use playeradded events at the start of a server, but it doesn’t work for some reason now…

Depending on what you do before you listen to PlayerAdded can delay it.

The issue is not with PlayerAdded not existing but PlayerAdded being fired before you have connected to it.

1 Like

Hm… I used an empty PlayerAdded event that just prints something, and it printed…:

image

Did you do that with your old code but with nothing inside? If yes, something inside of PlayerAdded is yielding the code. (Most likely to do with gravitymodule.addForce)

Yes. Interesting. I though modules ran once and then didn’t have to run again. How would I go about preventing the script from yielding?

Put prints in different parts of your current PlayerAdded and check what it last prints.

If it is the gravitymodule.addForce that is yielding it, please show unless you got it

I think you were right. It was ok all the way until the modulescript loop. I’m guessing it stopped because of errors, but it didn’t say so… weird. I’m still looking into it, though. I’ll give you an update.

Ah. It works. I just had to fix something in the module. Thank you very much.