Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

My gamepass/gui script still needs a little help? (Gave more detail)

repeat
game.Players:GetChildren()
if game:GetService("MarketplaceService"):PlayerOwnsAsset(player, 422504693)then
local  h = game.ServerStorage.ScreenGui:Clone()
 h.Parent = player.PlayerGui
end
until game.Workspace.Arrow.Anchored=false

I'm trying to get it so when a player buys my gamepass (In Game) The changes happen right away, and they don't have to rejoin. The screen GUI is the "Hints" players get when they buy the gamepass.

1 answer

Roblox provides all of the event needed to achieve this:-

PlayerAdded

CharacterAdded

PromptPurchaseFinished

This is an example only:-

local mpSrv = game:GetService('MarketplaceService') -- get the MarketplaceService
local srvSorage = game:GetService('ServerStorage'):WaitForChild('ScreenGui') -- this will 1st get the ServerStorage then wait till it fnds the ScreenGui 
local asset = 422504693 -- the purchase asset

game.Players.PlayerAdded:connect(function (plr)
	
	plr.CharacterAdded:connect(function (plrModel)
		local player = game.Players:GetPlayerFromCharacter(plrModel) -- finds the player associated with the players model
		if player then -- player found
			if mpSrv:PlayerOwnsAsset(player, asset) then
				local gui = plr:WaitForChild('PlayerGui') -- there may be a little delay so wait before we access it
				srvSorage:Clone().Parent = gui -- clones the gui to the players gui
			end
		end
	end)	
	
end)

-- mostly from the wiki
mpSrv.PromptPurchaseFinished:connect(function(plr, assetId, isPurchased)
	if isPurchased and assetId == asset then -- you must check that the putchase was a success and lets check the asses as well 
		-- as I assume a player does not have the gui lets add it here
		local gui = plr:WaitForChild('PlayerGui')
		srvSorage:Clone().Parent = gui
	end
end)

As I said this is only an example and it is un-tested. There are more efficient ways of doing this instead of checking each time if the player owns the asset.

Pls comment if you do not understand how this code works.

Hope this helps.

This discussion is preserved as it was. New answers, comments, and votes are closed.