Doesn't detect when a player joins

Hey Roblox devs. I have a script that is supposed to detect when a player joins, but it doesn’t. Either it doesn’t because the server script does not load in time when the player joins for it to detect the player joining, or I got some code wrong which is very unlikely. From what I can see after the tests I’ve ran, when I press play the server script loads AFTER the player joins, meaning it won’t detect the player joining. I’ve already tested this in a server with 2 players. Everytime a player joins I made the server print “joined,” and it only printed it out once.

2 Likes

Can i see the code?

(charrrrrrrrrr)

1 Like

That’s a common error I’ve seen, I hope this helps you:

1 Like
game.Players.PlayerAdded:Connect(function(PlayerAdded)
	print("joined")
	local GunEquip = PlayerAdded:WaitForChild("gunEvents").GunEquip
	local Blackout = game.ReplicatedStorage.guns.Blackout

	GunEquip.OnServerEvent:Connect(function(player, ARselected)
		if ARselected == ".300 AAC Blackout" then 
			local BlackoutClone = Blackout:Clone()
			BlackoutClone.Parent = player.Backpack
		end
	end)
end)

don’t worry about the “gunequip” stuff

I just looked over that, I don’t think that works for the context of my game. I also don’t see why it’s necessary to use a function for looping, or why it’s necessary for a “for _, player in pairs() do”, since all that’s doing is printing each individual player and that doesn’t help at all.

Local script:

game:GetService("Players").ChildAdded:Connect(function(player)
end)

Server side:

game:GetService("Players").PlayerAdded:Connect(function(player)
end)
GunEquip.OnServerEvent:Connect(function(player, ARselected)

Just so you’re aware, this is being connected each time PlayerAdded fires (memory leak).

1 Like

I think what you should do is loop through all the current players aswell because maybe the script didn’t load in time

Make sure there are no wait(…) before the playeradded runs.

does “joined” print when you join the game?