Script works only in studio and not in game

Making a flashlight gamepass and whenever a player joins it checks if he owns the pass and if he does it will give him the flashlight and print player owns the pass otherwise it won’t give him anything and it will print player does own the pass, So I bought the pass in game and rejoined and it prints player owns pass but it doesn’t give me the flashlight but when I tested it in studio it prints player owns it as well but it gives me the flashlight. So I am unsure why it doesn’t in game, Any idea?
Script:

local MPS = game:GetService("MarketplaceService")
local MPSID = 64276836
local Flashlight = game:GetService("ReplicatedStorage").Flashlight

game.Players.PlayerAdded:Connect(function(player)
		if MPS:UserOwnsGamePassAsync(player.UserId, MPSID) then
		Flashlight:Clone().Parent = player.Backpack
		print("Players that joined owns flashlight")
	else
		print("Player that joined does not own flashlight")
		end
end)
2 Likes

Maybe try using

player:WaitForChild("Backpack")
1 Like

I tried that recently it didn’t work, gave it another shot just now it didn’t work again so yeah doesn’t work

Could be MarketplaceService erroring. Does it print anything? Or perhaps you are just forgetting to publish the place.

Edit: Forget ignore the previous edit telling you to ignore this lol

It prints “Players that joined owns flashlight”, And no I didn’t forget to publish it since it prints the thing in the script so yeah it’s published

Use the normal way of cloning.

local Instance = something:Clone()
Instance.Parent = parent

The way you’re doing it fails for me 90% of the time, give it a shot.

1 Like

The problem is that it takes time to get the services, so the player already joined before the playeradded event is set. You have to loop through all players before the player added event and do the same thing. This is what i had to do in my game. Make the playeradded function seperate, so it can be called again in the loop.

He said it prints meaning the PlayerAdded fired, the issue you described happens when the event is connected too late aka below other yielding code so this isn’t the case.

What about when you use WaitForChild?

No? In one of my games I do it this way, and I’ve never seen it fail.

But he said that the prints do happen. What you say is correct though, that can happen as far as I know.

1 Like

WaitForChild where? The backpack? If the backpack was nil it would error not continue to print.

Instance:Clone().Parent == Parent

Qute literally never works for me it simply does not parent the object.
I’ll run a test now 1 moment.

No it wouldn’t error. We can set Instances’ parents to nil.

Correct actually oopsies. Let me run a test on that.

Neither worked, though you were correct the cloning way works, seems like the Backpack refreshes when the character fully loads, this fixed it.

game.Players.PlayerAdded:Connect(function(player)
   player.CharacterAppearanceLoaded:Wait()
   tool:Clone().Parent = player:WaitForChild("Backpack")
end)
2 Likes

Hello friends, this is because you will be adding the flashlight tool directly into the players backpack most likely before they even spawned in. This will also cause them to loose the flashlight once they respawn anyway.

Instead you should simply be:

  • Wait for player added

  • Do they own the gamepass?

  • If yes, add the gamepass to their startergear

  • Are they already spawned in?

  • If yes, also clone it to their backpack directly.

local MPS = game:GetService("MarketplaceService")
local Flashlight = game:GetService("ReplicatedStorage").Flashlight
local PassID = 64276836

game.Players.PlayerAdded:Connect(function(Player)
	if MPS:UserOwnsGamePassAsync(Player.UserId, PassID) then
		Flashlight:Clone().Parent = Player.StarterGear
		
		if Player.Character then
			Flashlight:Clone().Parent = Player.Backpack
		end
	end
end)
1 Like

Non of the ideas you have suggested and the others have worked, Thanks to all of you for trying at least!

Well it thankfully works, Tysm!