Need help with script

Hello! I am a basic Lua scripter, and are running into some problems. If you help me it would be greatly appreciated.

So I am making a gamepass team change, and I am encountering some problems with the script.

As you can see, for my team change, if the player does not own the gamepass, a GUI pops up over the team change, saying that they don’t own the correct gamepass. I have tried the script and changed some things over and over, but it hasn’t worked.

Down below, you can see the orders. I want to make it if the player owns the certain gamepass, “Unowned” is not visible.
ec6b4dcbff2b549fe538d9352806dc79

Please look down below for the current script.



script.Parent.MouseEnter:Connect(function()
	script.Parent.BackgroundColor3 = Color3.new(255/255, 200/255, 0/255)
end)

script.Parent.MouseLeave:Connect(function()
	script.Parent.BackgroundColor3 = Color3.new(11/255, 11/255, 11/255)
end)

	local ownsGamepass = game:GetService("MarketplaceService):UserOwnsGamePassAsync(plr.UserId,99509845)
	
	if ownsGamepass then
	script.Parent.Unowned.Visible = false
end

Help would be greatly appreciated. Thank you!

Dont do this, :UserOwnsGamePassAsync() is used as a if statement

local MS = game:GetService("MarketplaceService")
script.Parent.Activated:Connect(function()
if MS:UserOwnsGamePassAsync(plr.UserId,99509845) then
	script.Parent.Unowned.Visible = false
end
end)

Thank you so much! I will try it right now.

Would I just put it right under

script.Parent.MouseEnter:Connect(function()
script.Parent.BackgroundColor3 = Color3.new(255/255, 200/255, 0/255)
end)

script.Parent.MouseLeave:Connect(function()
script.Parent.BackgroundColor3 = Color3.new(11/255, 11/255, 11/255)
end)

you could

Maybe something like this:

script.Parent.MouseEnter:Connect(function()
        if MS:UserOwnsGamePassAsync(plr.UserId,99509845) then
           script.Parent.BackgroundColor3 = Color3.new(255/255, 200/255, 0/255)

       script.Parent.MouseLeave:Connect(function()
       script.Parent.BackgroundColor3 = Color3.new(11/255, 11/255, 11/255)
             end)
    end
end)


thank you so much! i really appreciate it.

if this solved it, mark it as the Solution

Wait. I added the script, and the text over it did not delete.

I own the gamepass as well. Any solutions?

And I have a doubt UserOwnsGamePassAsync() should not be wrapped in a pcall ?

Not Exactly. UserOwnsGamePassAsync() simply checks if a Player owns a gamepass, im not sure why you would need a pcall for that

Because many checks of that type, related with datastores, marketplace, etc, could fail due to roblox issues, if that query never fails, then its ok.

according to the dev forum, the method yields (the async part also tells us that). I always recommend putting a pcall on a yielding method in the event it does go down and users are in your game

I dont know if this can be helpful but i did something like this with some emotes for my game, and when i buyed the gamepass the gui didnt set the visible property to false so u could click the emote u wanted, so i just tried to reset and it worked, try to reset after u buyed it