Players.PlayerAdded doesn't fire for some reason

  1. What do you want to achieve?

When the Player joins, the game checks if Player has a gamepass. If Player has gamepass, it gives them the radio.

  1. What is the issue?

Players.PlayerAdded isn’t working/doesn’t fire when I start my game.

local MarketplaceService = game:GetService("MarketplaceService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Event = ReplicatedStorage:WaitForChild("RadioEvent")
local StarterGui = game:GetService("StarterGui")
local Players = game:GetService("Players")
local Radio = RadioSongGui.Radio
local RadioBrick = workspace.Folder.RadioGamePassBrick
local GamepassID = 24358592

local function GiveRadio(Player)
	local Character = Player.Character or Player.CharacterAdded:Wait()
	local newRadio = Radio:Clone()
	newRadio.Parent = Character
	newRadio.CFrame = Character.Torso.CFrame * CFrame.Angles(0, math.pi, 0)
	local weld = Instance.new("Weld")
	weld.Part0 = newRadio
	weld.Part1 = Character.Torso
	weld.C0 = newRadio.CFrame:inverse()
	weld.C1 =  Character.Torso.CFrame:inverse()
	weld.Parent = newRadio
	
	Event:FireClient(Player)
end

local function OwnsGamepass(Player, Gamepass_ID)
	return MarketplaceService:UserOwnsGamePassAsync(Player.UserId, Gamepass_ID)
end

local function CheckIfOwnsGamepass(Player)
	print("TESTING")
	Player.CharacterAdded:Connect(function(character)
		if OwnsGamepass(character, GamepassID) then
			GiveRadio(character)
		end
	end)
end

local function GamepassPurchased(Player, PurchasedGamepassID, PurchasedValue)
	
	if PurchasedValue then
		
		if PurchasedGamepassID == GamepassID then
			
			GiveRadio(Player)
		end
	end
end

Players.PlayerAdded:Connect(CheckIfOwnsGamepass)
RadioBrick.Touched:Connect(PromptPurchase)
MarketplaceService.PromptGamePassPurchaseFinished:Connect(GamepassPurchased)

Can you try joining in roblox normale game? Since PlayerAdded doest not fire always on studio.

In your CheckIfOwnsGamePass function you call the OwnsGamepass function with the character as your first argument.
However: Your OwnsGamePass function takes the player in as its first argument and not the character.

To fix this:

local function CheckIfOwnsGamepass(Player)
	print("TESTING")
	Player.CharacterAdded:Connect(function(character)
		if OwnsGamepass(Player, GamepassID) then
			GiveRadio(Player)
		end
	end)
end

Edit: My bad, you don’t need to use the GetPlayerFromCharacter function, since the player is already passed through in the function.

Players.PlayerAdded still didn’t get fired, all the other connections work perfectly except Players.PlayerAdded. If it actually got fired, it’s supposed to be print “TESTING” but unfortunately, it doesn’t.

The reason why this is happening is because the player joins the game before the connection is established, most likely because something is yielding the execution of the script until after a player has joined. To overcome this obstacle, you can loop through the current players in the game and call the playerAdded function before creating the connection.

for _, player in Players:GetPlayers() do
   task.spawn(function()
      CheckIfOwnsGamepass(player)
   end)
end
Players.PlayerAdded:Connect(CheckIfOwnsGamepass)
RadioBrick.Touched:Connect(PromptPurchase)
MarketplaceService.PromptGamePassPurchaseFinished:Connect(GamepassPurchased)
3 Likes

Oh man, I couldn’t understand the reason on my own. now I understand it! and the code actually works, Thank you!!

Is this a local script? Because if it is then the ‘PlayerAdded’ signal doesn’t fire for the local player.

2 Likes