Script not Detecting Whether a Player Owns a Gamepass or Not

I have a LocalScript in StarterCharacterScripts that is supposed to detect whether a player owns a specific gamepass or not. If they do own the gamepass, it’ll give them a rainbow trail instantly upon joining (or respawning). However, I’m having trouble detecting if they own it or not.

  1. What do you want to achieve? I want to give the player a rainbow trail instantly when their character spawns if they own a gamepass.

  2. What is the issue? I can’t detect if they own the gamepass.

  3. What solutions have you tried so far? I have found a topic that explains what I want to do, but it still does not solve my issue.

This is the script.
local market = game:GetService("MarketplaceService")
local players = game:GetService("Players")
local player = players.LocalPlayer
local hrp = player.Character:WaitForChild("HumanoidRootPart")
local att0 = hrp:WaitForChild("Attachment0")
local att1 = hrp:WaitForChild("Attachment1")
local trail = hrp:WaitForChild("Trail")
local userid = player.UserId
local passid = 7994577

player.CharacterAdded:Connect(function()
    local ownpass = market:UserOwnsGamePassAsync(userid, passid)
	--If they own the pass already
    if ownpass then
    	print("Here's a free rainbow trail!")
    	local rtrail = game.ReplicatedStorage.RainbowTrail:Clone()
    	trail.Enabled = false
    	rtrail.Attachment0 = att0
	    rtrail.Attachment1 = att1
    	rtrail.Enabled = true
    	rtrail.Parent = hrp
    else
    	--If they don't own the pass already
    	print("Buy a rainbow trail yourself.")
	end
end)

I know that it not detecting the player owning the gamepass because nothing appears in the output. Not “Here’s a free rainbow trail!” nor “Buy a rainbow trail yourself.” No errors either. The lines in the script that give them the rainbow trail should work perfectly fine, because I tested it when the player clicks an ImageButton.

Anyways, any help is appreciated. :slightly_smiling_face:

2 Likes

Thanks for linking in my thread. I have read your code and believe I’ve found a way in order to make it work.

local players = game:GetService("Players")
local player = players.LocalPlayer
local hrp = player.Character:WaitForChild("HumanoidRootPart")
local att0 = hrp:WaitForChild("Attachment0")
local att1 = hrp:WaitForChild("Attachment1")
local trail = hrp:WaitForChild("Trail")
local userid = player.UserId
local passid = 7994577

player.CharacterAdded:Connect(function()

if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(Player.UserId, 7994577) then
print("Here's a free rainbow trail!")
    	local rtrail = game.ReplicatedStorage.RainbowTrail:Clone()
    	trail.Enabled = false
    	rtrail.Attachment0 = att0
	    rtrail.Attachment1 = att1
    	rtrail.Enabled = true
    	rtrail.Parent = hrp
  end
end)

I haven’t tested this but using my own code I believe that it should. Correct me if I’m wrong - it’s getting late and I’m tired.

1 Like

It does not work. It seems like you just replaced the MarketplaceService variables with what the variables were assigned to, or something.

You want to be doing this with a server script. The connection to the CharacterAdded event is likely not being created client side until after the character is added from respawning so your code doesn’t run. Also, if you want the trail to show for all players, you will have to add it sever side.

I have tried this script with Players.PlayerAdded as well, and it still didn’t work.

Edit: Nevermind. I used a RemoteEvent, and it worked.

1 Like

I’m just going to point this out, if you created the rainbow trail only on the client then it’s not being replicated to the server. If you do this on the server it just theoretically work. Heres some proto code. (Move the RainbowTrail from ReplicatedStorage to ServerStorage)

local Market, Players = game:GetService("MarketplaceService"), game:GetService("Players")

local passId = 7994577

Players.PlayerAdded:Connect(function(player)
    local ownpass = Market:UserOwnsGamePassAsync(player.UserId, passId)
	--If they own the pass already
    if (ownpass) then
    	print("Here's a free rainbow trail!")

        local hrp = player.Character:WaitForChild("HumanoidRootPart")
    	local att0 = hrp:WaitForChild("Attachment0")
    	local att1 = hrp:WaitForChild("Attachment1")
    	local trail = hrp:WaitForChild("Trail")

    	local rtrail = game.ServerStorage.RainbowTrail:Clone()

    	trail.Enabled = false
    	rtrail.Attachment0 = att0
	rtrail.Attachment1 = att1
    	rtrail.Enabled = true
    	rtrail.Parent = hrp
    else
    	--If they don't own the pass already
    	print("Buy a rainbow trail yourself.")
    end
end)

For some reason, children of ServerStorage (and ServerScriptService) remove themselves out of the game, yet I have no clue why. So I use ReplicatedStorage as a substitute.

Are you sure you viewing from the server side or the client side? Roblox studio has a image and you can flip that

1 Like

Ah. I did not know that. Thank you.