Is there a better way to make this shop server?

So I have made a fully working title shop and would like it if some of you guys would review my script. I feel like it is very long and there has to be a better way of doing this!

game.Players.PlayerAdded:Connect(function(player)
	
	game.Workspace.ChildAdded:Connect(function(character)
		
		local ServerStorage = game:GetService('ServerStorage')
		local ReplicatedStorage = game:GetService('ReplicatedStorage')
		
		local CosmeticEvents = ReplicatedStorage:WaitForChild('CosmeticEvents')

		local PlayerStats = player:WaitForChild('PlayerStats')

		--//Stats\\--

		local Coins = PlayerStats:WaitForChild('Coins')

		--//Cosmetics\\--
		
		local nameTag = game:GetService('ServerStorage'):WaitForChild('NameTag')
		local currentTag

		local NoviceTitle = PlayerStats:WaitForChild('NoviceTitle')
		local RookieTitle = PlayerStats:WaitForChild('RookieTitle')
		local BeginnerTitle = PlayerStats:WaitForChild('BeginnerTitle')
		local AmatuerTitle = PlayerStats:WaitForChild('AmatuerTitle')
		local IntermediateTitle = PlayerStats:WaitForChild('IntermediateTitle')
		local TalentedTitle = PlayerStats:WaitForChild('TalentedTitle')
		local SkilledTitle = PlayerStats:WaitForChild('SkilledTitle')
		local SeasonedTitle = PlayerStats:WaitForChild('SeasonedTitle')
		local ProficientTitle = PlayerStats:WaitForChild('ProficientTitle')
		local ExperiencedTitle = PlayerStats:WaitForChild('ExperiencedTitle')
		local AdvancedTitle = PlayerStats:WaitForChild('AdvancedTitle')
		local SeniorTitle = PlayerStats:WaitForChild('SeniorTitle')
		local ExpertTitle = PlayerStats:WaitForChild('ExpertTitle')
		local OwnerTitle = PlayerStats:WaitForChild('OwnerTitle')

		--//Remote Events\\--

		local NoviceTitleEvent = CosmeticEvents:WaitForChild('NoviceTitleEvent')
		local RookieTitleEvent = CosmeticEvents:WaitForChild('RookieTitleEvent')
		local BeginnerTitleEvent = CosmeticEvents:WaitForChild('BeginnerTitleEvent')
		local AmatuerTitleEvent = CosmeticEvents:WaitForChild('AmatuerTitleEvent')
		local IntermediateTitleEvent = CosmeticEvents:WaitForChild('IntermediateTitleEvent')
		local TalentedTitleEvent = CosmeticEvents:WaitForChild('TalentedTitleEvent')
		local SkilledTitleEvent = CosmeticEvents:WaitForChild('SkilledTitleEvent')
		local SeasonedTitleEvent = CosmeticEvents:WaitForChild('SeasonedTitleEvent')
		local ProficientTitleEvent = CosmeticEvents:WaitForChild('ProficientTitleEvent')
		local ExperiencedTitleEvent = CosmeticEvents:WaitForChild('ExperiencedTitleEvent')
		local AdvancedTitleEvent = CosmeticEvents:WaitForChild('AdvancedTitleEvent')
		local SeniorTitleEvent = CosmeticEvents:WaitForChild('SeniorTitleEvent')
		local ExpertTitleEvent = CosmeticEvents:WaitForChild('ExpertTitleEvent')
		local OwnerTitleEvent = CosmeticEvents:WaitForChild('OwnerTitleEvent')
		
		local clone = nameTag:Clone()
		clone.Enabled = true
		clone.Parent = character.Head
		clone.Frame.TextLabel.Text = character.Name

		if player.MembershipType == Enum.MembershipType.Premium then

			clone.Frame.TextLabel.TextColor3 = Color3.fromRGB(70, 70, 70)
			clone.Frame.TextLabel.Text = '✨Premium '..character.Name
		end

		if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(player.UserId,27136394) then

			clone.Frame.TextLabel.TextColor3 = Color3.fromRGB(255, 255, 0)
			clone.Frame.TextLabel.Text = '👑VIP '..character.Name
		end	
		
		if player.UserId == 514422896 then
			
			clone.Frame.TextLabel.Text = clone.Frame.TextLabel.Text..'\n'..[[<font color="rgb(255,0,0)">Owner</font>]]
			currentTag = clone.Frame.TextLabel.Text
		else
			
			clone.Frame.TextLabel.Text = clone.Frame.TextLabel.Text..'\n'..[[<font color="rgb(255,255,0)">Noob</font>]]
			currentTag = clone.Frame.TextLabel.Text
		end
	
		NoviceTitleEvent.OnServerEvent:Connect(function()

			if NoviceTitle.Value == true then
				
				clone.Frame.TextLabel.Text = clone.Frame.TextLabel.Text..'\n'..[[<font color="rgb(255,255,255)">Novice</font>]]
			
			elseif Coins.Value >= 500 and NoviceTitle.Value == false then

				Coins.Value -= 500
				clone.Frame.TextLabel.Text = clone.Frame.TextLabel.Text..'\n'..[[<font color="rgb(255,255,255)">Novice</font>]]
				NoviceTitle.Value = true

			end
		end)
		
		RookieTitleEvent.OnServerEvent:Connect(function()

			if RookieTitle.Value == true then

				clone.Frame.TextLabel.Text = clone.Frame.TextLabel.Text..'\n'..[[<font color="rgb(255,255,180)">Rookie</font>]]

			elseif Coins.Value >= 1000 and RookieTitle.Value == false then

				Coins.Value -= 1000
				clone.Frame.TextLabel.Text = clone.Frame.TextLabel.Text..'\n'..[[<font color="rgb(255,255,180)">Rookie</font>]]
				RookieTitle.Value = true
				
			end
		end)
		
		BeginnerTitleEvent.OnServerEvent:Connect(function()

			if BeginnerTitle.Value == true then

				clone.Frame.TextLabel.Text = clone.Frame.TextLabel.Text..'\n'..[[<font color="rgb(130,193,255)">Beginner</font>]]

			elseif Coins.Value >= 1500 and BeginnerTitle.Value == false then

				Coins.Value -= 1500
				clone.Frame.TextLabel.Text = clone.Frame.TextLabel.Text..'\n'..[[<font color="rgb(130,193,255)">Beginner</font>]]
				BeginnerTitle.Value = true
				
			end
		end)
		
		AmatuerTitleEvent.OnServerEvent:Connect(function()

			if AmatuerTitle.Value == true then

				clone.Frame.TextLabel.Text = clone.Frame.TextLabel.Text..'\n'..[[<font color="rgb(255,132,130)">Amatuer</font>]]

			elseif Coins.Value >= 2000 and AmatuerTitle.Value == false then

				Coins.Value -= 2000
				clone.Frame.TextLabel.Text = clone.Frame.TextLabel.Text..'\n'..[[<font color="rgb(255,132,130)">Amatuer</font>]]
				AmatuerTitle.Value = true
				
			end
		end)
		
		IntermediateTitleEvent.OnServerEvent:Connect(function()

			if IntermediateTitle.Value == true then

				clone.Frame.TextLabel.Text = clone.Frame.TextLabel.Text..'\n'..[[<font color="rgb(105,255,223)">Intermediate</font>]]

			elseif Coins.Value >= 3000 and IntermediateTitle.Value == false then

				Coins.Value -= 3000
				clone.Frame.TextLabel.Text = clone.Frame.TextLabel.Text..'\n'..[[<font color="rgb(105,255,223)">Intermediate</font>]]
				IntermediateTitle.Value = true
				
			end
		end)
		
		TalentedTitleEvent.OnServerEvent:Connect(function()

			if TalentedTitle.Value == true then

				clone.Frame.TextLabel.Text = clone.Frame.TextLabel.Text..'\n'..[[<font color="rgb(255,130,166)">Talented</font>]]

			elseif Coins.Value >= 4000 and TalentedTitle.Value == false then

				Coins.Value -= 4000
				clone.Frame.TextLabel.Text = clone.Frame.TextLabel.Text..'\n'..[[<font color="rgb(255,130,166)">Talented</font>]]
				TalentedTitle.Value = true
				
			end
		end)
		
		SkilledTitleEvent.OnServerEvent:Connect(function()

			if SkilledTitle.Value == true then

				clone.Frame.TextLabel.Text = clone.Frame.TextLabel.Text..'\n'..[[<font color="rgb(255,150,80)">Skilled</font>]]

			elseif Coins.Value >= 5000 and SkilledTitle.Value == false then

				Coins.Value -= 5000
				clone.Frame.TextLabel.Text = clone.Frame.TextLabel.Text..'\n'..[[<font color="rgb(255,150,80)">Skilled</font>]]
				SkilledTitle.Value = true
				
			end
		end)
		
		SeasonedTitleEvent.OnServerEvent:Connect(function()

			if SeasonedTitle.Value == true then

				clone.Frame.TextLabel.Text = clone.Frame.TextLabel.Text..'\n'..[[<font color="rgb(0,150,255)">Seasoned</font>]]

			elseif Coins.Value >= 7500 and SeasonedTitle.Value == false then

				Coins.Value -= 7500
				clone.Frame.TextLabel.Text = clone.Frame.TextLabel.Text..'\n'..[[<font color="rgb(0,150,255)">Seasoned</font>]]
				SeasonedTitle.Value = true
				
			end
		end)
		
		ProficientTitleEvent.OnServerEvent:Connect(function()

			if ProficientTitle.Value == true then

				clone.Frame.TextLabel.Text = clone.Frame.TextLabel.Text..'\n'..[[<font color="rgb(80,255,80)">Proficient</font>]]

			elseif Coins.Value >= 10000 and ProficientTitle.Value == false then

				Coins.Value -= 10000
				clone.Frame.TextLabel.Text = clone.Frame.TextLabel.Text..'\n'..[[<font color="rgb(80,255,80)">Proficient</font>]]
				ProficientTitle.Value = true
				
			end
		end)
		
		ExperiencedTitleEvent.OnServerEvent:Connect(function()

			if ExperiencedTitle.Value == true then

				clone.Frame.TextLabel.Text = clone.Frame.TextLabel.Text..'\n'..[[<font color="rgb(208,160,255)">Experienced</font>]]

			elseif Coins.Value >= 15000 and ExperiencedTitle.Value == false then

				Coins.Value -= 12500
				clone.Frame.TextLabel.Text = clone.Frame.TextLabel.Text..'\n'..[[<font color="rgb(208,160,255)">Experienced</font>]]
				ExperiencedTitle.Value = true
				
			end
		end)
		
		AdvancedTitleEvent.OnServerEvent:Connect(function()

			if AdvancedTitle.Value == true then

				clone.Frame.TextLabel.Text = clone.Frame.TextLabel.Text..'\n'..[[<font color="rgb(255,0,127)">Advanced</font>]]

			elseif Coins.Value >= 15000 and AdvancedTitle.Value == false then

				Coins.Value -= 15000
				clone.Frame.TextLabel.Text = clone.Frame.TextLabel.Text..'\n'..[[<font color="rgb(255,0,127)">Advanced</font>]]
				AdvancedTitle.Value = true
				
			end
		end)
		
		SeniorTitleEvent.OnServerEvent:Connect(function()

			if SeniorTitle.Value == true then

				clone.Frame.TextLabel.Text = clone.Frame.TextLabel.Text..'\n'..[[<font color="rgb(135,90,67)">Senior</font>]]

			elseif Coins.Value >= 17500 and SeniorTitle.Value == false then

				Coins.Value -= 17500
				clone.Frame.TextLabel.Text = clone.Frame.TextLabel.Text..'\n'..[[<font color="rgb(135,90,67)">Senior</font>]]
				SeniorTitle.Value = true
				
			end
		end)
		
		ExpertTitleEvent.OnServerEvent:Connect(function()

			if ExpertTitle.Value == true then

				clone.Frame.TextLabel.Text = clone.Frame.TextLabel.Text..'\n'..[[<font color="rgb(150,0,0)">Expert</font>]]

			elseif Coins.Value >= 20000 and ExpertTitle.Value == false then

				Coins.Value -= 20000
				clone.Frame.TextLabel.Text = clone.Frame.TextLabel.Text..'\n'..[[<font color="rgb(150,0,0)">Expert</font>]]
				ExpertTitle.Value = true
				
			end
		end)
		
		game:GetService("MarketplaceService").PromptGamePassPurchaseFinished:Connect(function(player, id, purchased)

			if id == 27136394 and purchased then

				clone.Frame.TextLabel.TextColor3 = Color3.fromRGB(255, 255, 0)
				clone.Frame.TextLabel.Text = '👑VIP '..currentTag
			end
		end)
	end)	
end)

Any recommendations are appreciated! (I can provide a place file if this is to hard to read.)

Basically there is a name tag that gets set to the players head when they join. Then if they have premium, the tag is set to say Premium and then the players name. If they own VIP then it is set to say VIP and the players name. From a shop they are able to purchase a title which fires the remote event to here. Then this script handles them buying the tag and equipping it. At the end of the script it also handles if they buy VIP in game.

1 Like

There is a better way. You can use a single title shop event then have that event take an argument then it find whatever title is specified in the argument in the table then reads the data from that table.

local titles = {
    Title1 = {
        Cost = 1000;
        Text = '<font color="rgb(255,255,180)">Title1</font>'
   };
}
local TitleEvent = game.ReplicatedStorage.TitleEvent
TitleEvent.OnServerEvent:Connect(function(player,title1)
    local PlayerStats = player:WaitForChild('PlayerStats')
    local title = titles[title1]
    if title then
        if PlayerStats.Coins.Value >=  title.Cost then
            PlayerStats.CurrentTitle = title.Text
        end
    end
end)
game.Players.PlayerAdded:Connect(function(player)
   --Your PlayerAdded code
end)

The PlayerAdded section would mostly be the same but it would go off of the CurrentTitle section so you don’t have all those remote events. You could also store the owned titles in a table and see if the title is in that table and prevent the player from buying it.
Using your current system, an exploiter could fire those remote events to purposefully drain all the money that the player has to make them rage quit.

2 Likes

Ok so I think I have applied what you have told me correctly:

--//Services\\--

local ServerStorage = game:GetService('ServerStorage')
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local MarketplaceService = game:GetService('MarketplaceService')

--//Title Table\\--

local titles = {
	NoviceTitle = {
		Cost = 500;
		Text = '\n'..[[<font color="rgb(255,255,255)">Novice</font>]]
	};
	RookieTitle = {
		Cost = 1000;
		Text = '\n'..[[<font color="rgb(255,255,180)">Rookie</font>]]
	};
	BeginnerTitle = {
		Cost = 1500;
		Text = '\n'..[[<font color="rgb(130,193,255)">Beginner</font>]]
	};
	AmatuerTitle = {
		Cost = 2000;
		Text = '\n'..[[<font color="rgb(255,132,130)">Amatuer</font>]]
	};
	IntermediateTitle = {
		Cost = 3000;
		Text = '\n'..[[<font color="rgb(105,255,223)">Intermediate</font>]]
	};
	TalentedTitle = {
		Cost = 4000;
		Text = '\n'..[[<font color="rgb(255,130,166)">Talented</font>]]
	};
	SkilledTitle = {
		Cost = 5000;
		Text = '\n'..[[<font color="rgb(255,150,80)">Skilled</font>]]
	};
	SeasonedTitle = {
		Cost = 7500;
		Text = '\n'..[[<font color="rgb(0,150,255)">Seasoned</font>]]
	};
	ProficientTitle = {
		Cost = 10000;
		Text = '\n'..[[<font color="rgb(80,255,80)">Proficient</font>]]
	};
	ExperiencedTitle = {
		Cost = 12500;
		Text = '\n'..[[<font color="rgb(208,160,255)">Experienced</font>]]
	};
	AdvancedTitle = {
		Cost = 15000;
		Text = '\n'..[[<font color="rgb(255,0,127)">Advanced</font>]]
	};
	SeniorTitle = {
		Cost = 17500;
		Text = '\n'..[[<font color="rgb(135,90,67)">Senior</font>]]
	};
	ExpertTitle = {
		Cost = 20000;
		Text = '\n'..[[<font color="rgb(150,0,0)">Expert</font>]]
	};
}

--//Title Buy Event\\--

local BuyTitleEvent = ReplicatedStorage.BuyTitle

BuyTitleEvent.OnServerEvent:Connect(function(player, titleInfo)

	local PlayerStats = player:WaitForChild('PlayerStats')
	
	local NameTag = player.Character.Head.NameTag.Frame.TextLabel
	local title = titles[titleInfo]

	if title then
		if PlayerStats.Coins.Value >=  title.Cost then
			
			CurrentTitle = title.Text
			
			if player.MembershipType == Enum.MembershipType.Premium then

				if MarketplaceService:UserOwnsGamePassAsync(player.UserId,27136394) then

					NameTag.TextColor3 = Color3.fromRGB(255, 255, 0)
					NameTag.Text = '👑VIP '..player.Name..CurrentTitle
				else

					NameTag.TextColor3 = Color3.fromRGB(70, 70, 70)
					NameTag.Text = '✨Premium '..player.Name..CurrentTitle
				end
			else
				
				NameTag.Text = player.Name..CurrentTitle
			end
			--Check if they are VIP but not Premium. Improvements?
			if MarketplaceService:UserOwnsGamePassAsync(player.UserId,27136394) then

				NameTag.TextColor3 = Color3.fromRGB(255, 255, 0)
				NameTag.Text = '👑VIP '..player.Name..CurrentTitle
			end
		end
	end
end)

--//Player Added\\--

game.Players.PlayerAdded:Connect(function(player)
	game.Workspace.ChildAdded:Connect(function(character)
		
		if character.Head:FindFirstChild('NameTag') then
			print(player.Name..' already has a tag.')
		else
			local tag = ServerStorage:WaitForChild('NameTag')

			local clone = tag:Clone()
			clone.Enabled = true
			clone.Parent = character.Head
			clone.Frame.TextLabel.Text = character.Name
			
			if player.MembershipType == Enum.MembershipType.Premium then

				if MarketplaceService:UserOwnsGamePassAsync(player.UserId,27136394) then

					clone.Frame.TextLabel.TextColor3 = Color3.fromRGB(255, 255, 0)
					clone.Frame.TextLabel.Text = '👑VIP '..character.Name
				else

					clone.Frame.TextLabel.TextColor3 = Color3.fromRGB(70, 70, 70)
					clone.Frame.TextLabel.Text = '✨Premium '..character.Name
				end
			end
			--Check if they are VIP but not Premium. Improvements?
			if MarketplaceService:UserOwnsGamePassAsync(player.UserId,27136394) then

				clone.Frame.TextLabel.TextColor3 = Color3.fromRGB(255, 255, 0)
				clone.Frame.TextLabel.Text = '👑VIP '..character.Name
			end

			if player.UserId == 514422896 then

				clone.Frame.TextLabel.Text = clone.Frame.TextLabel.Text..'\n'..[[<font color="rgb(255,0,0)">Owner</font>]]
				CurrentTitle = '\n'..[[<font color="rgb(255,0,0)">Owner</font>]]
			else

				clone.Frame.TextLabel.Text = clone.Frame.TextLabel.Text..'\n'..[[<font color="rgb(255,255,0)">Noob</font>]]
				CurrentTitle = '\n'..[[<font color="rgb(255,255,0)">Noob</font>]]
			end	
		end		
	end)
end)

local function onPromptGamePassPurchaseFinished(player, id, purchase)
	
	if purchase == true and id == 27136394 then
		
		local NameTag = player.Character.Head.NameTag.Frame.TextLabel
		
		NameTag.TextColor3 = Color3.fromRGB(255, 255, 0)
		NameTag.Text = '👑VIP '..player.Name..CurrentTitle
	end
end

MarketplaceService.PromptGamePassPurchaseFinished:Connect(onPromptGamePassPurchaseFinished)

My question is how would I go about firing the info to the server?

game.ReplicatedStorage.BuyTitle:FireServer()--What should I send here?

Also I feel like there is a better way to check if they are premium, then VIP, then if they aren’t premium check if they are VIP:

			if player.MembershipType == Enum.MembershipType.Premium then

				if MarketplaceService:UserOwnsGamePassAsync(player.UserId,27136394) then

					clone.Frame.TextLabel.TextColor3 = Color3.fromRGB(255, 255, 0)
					clone.Frame.TextLabel.Text = '👑VIP '..character.Name
				else

					clone.Frame.TextLabel.TextColor3 = Color3.fromRGB(70, 70, 70)
					clone.Frame.TextLabel.Text = '✨Premium '..character.Name
				end
			end
			--Check if they are VIP but not Premium. Improvements?
			if MarketplaceService:UserOwnsGamePassAsync(player.UserId,27136394) then

				clone.Frame.TextLabel.TextColor3 = Color3.fromRGB(255, 255, 0)
				clone.Frame.TextLabel.Text = '👑VIP '..character.Name
			end

And then I can change the check when they purchase a title also.

1 Like

With FireServer, you put the arguments in the parenthesis like you are firing a function. Player is already specified though. If you want to buy Title1, you do

game.replicatedstorage.buytitle:FireServer(“Title1”)
2 Likes

Ahh ok that makes sense. So this is the script that I have been able to come up with, I was wondering if there is anyway to improve the checks for VIP or premium.

--//Services\\--

local ServerStorage = game:GetService('ServerStorage')
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local MarketplaceService = game:GetService('MarketplaceService')

--//Title Table\\--

local titles = {
	NoobTitle = {
		Name = 'NoobTitle';
		Cost = 500;
		Text = '\n'..[[<font color="rgb(255,255,0)">Noob</font>]]
	};
	NoviceTitle = {
		Name = 'NoviceTitle';
		Cost = 500;
		Text = '\n'..[[<font color="rgb(255,255,255)">Novice</font>]]
	};
	RookieTitle = {
		Name = 'RookieTitle';
		Cost = 1000;
		Text = '\n'..[[<font color="rgb(255,255,180)">Rookie</font>]]
	};
	BeginnerTitle = {
		Name = 'BeginnerTitle';
		Cost = 1500;
		Text = '\n'..[[<font color="rgb(130,193,255)">Beginner</font>]]
	};
	AmatuerTitle = {
		Name = 'AmatuerTitle';
		Cost = 2000;
		Text = '\n'..[[<font color="rgb(255,132,130)">Amatuer</font>]]
	};
	IntermediateTitle = {
		Name = 'IntermediateTitle';
		Cost = 3000;
		Text = '\n'..[[<font color="rgb(105,255,223)">Intermediate</font>]]
	};
	TalentedTitle = {
		Name = 'TalentedTitle';
		Cost = 4000;
		Text = '\n'..[[<font color="rgb(255,130,166)">Talented</font>]]
	};
	SkilledTitle = {
		Name = 'SkilledTitle';
		Cost = 5000;
		Text = '\n'..[[<font color="rgb(255,150,80)">Skilled</font>]]
	};
	SeasonedTitle = {
		Name = 'SeasonedTitle';
		Cost = 7500;
		Text = '\n'..[[<font color="rgb(0,150,255)">Seasoned</font>]]
	};
	ProficientTitle = {
		Name = 'ProficientTitle';
		Cost = 10000;
		Text = '\n'..[[<font color="rgb(80,255,80)">Proficient</font>]]
	};
	ExperiencedTitle = {
		Name = 'ExperiencedTitle';
		Cost = 12500;
		Text = '\n'..[[<font color="rgb(208,160,255)">Experienced</font>]]
	};
	AdvancedTitle = {
		Name = 'AdvancedTitle';
		Cost = 15000;
		Text = '\n'..[[<font color="rgb(255,0,127)">Advanced</font>]]
	};
	SeniorTitle = {
		Name = 'SeniorTitle';
		Cost = 17500;
		Text = '\n'..[[<font color="rgb(135,90,67)">Senior</font>]]
	};
	ExpertTitle = {
		Name = 'ExpertTitle';
		Cost = 20000;
		Text = '\n'..[[<font color="rgb(150,0,0)">Expert</font>]]
	};
}

--//Title Buy Event\\--

local BuyTitleEvent = ReplicatedStorage.BuyTitle

BuyTitleEvent.OnServerEvent:Connect(function(player, titleInfo)

	local PlayerStats = player:WaitForChild('PlayerStats')
	
	local NameTag = player.Character.Head.NameTag.Frame.TextLabel
	local title = titles[titleInfo]

	if title then
		local titleName = title.Name
		if PlayerStats:FindFirstChild(titleName).Value == true then
			
			print(player.Name..' Already owns '..titleName)
		else
			
			if PlayerStats.Coins.Value >=  title.Cost then

				CurrentTitle = title.Text

				PlayerStats.Coins.Value -= title.Cost

				PlayerStats:FindFirstChild(titleName).Value = true

				if player.MembershipType == Enum.MembershipType.Premium then

					if MarketplaceService:UserOwnsGamePassAsync(player.UserId,27136394) then

						NameTag.TextColor3 = Color3.fromRGB(255, 255, 0)
						NameTag.Text = '👑VIP '..player.Name..CurrentTitle
					else

						NameTag.TextColor3 = Color3.fromRGB(70, 70, 70)
						NameTag.Text = '✨Premium '..player.Name..CurrentTitle
					end
				else

					NameTag.Text = player.Name..CurrentTitle
				end
				--Check if they are VIP but not Premium. Improvements?
				if MarketplaceService:UserOwnsGamePassAsync(player.UserId,27136394) then

					NameTag.TextColor3 = Color3.fromRGB(255, 255, 0)
					NameTag.Text = '👑VIP '..player.Name..CurrentTitle
				end
			end
		end
	end
end)

--//Player Added\\--

game.Players.PlayerAdded:Connect(function(player)	
	game.Workspace.ChildAdded:Connect(function(character)
		
		if character.Head:FindFirstChild('NameTag') then
			print(player.Name..' already has a nametag.')
			
		else
			local tag = ServerStorage:WaitForChild('NameTag')

			local clone = tag:Clone()
			clone.Enabled = true
			clone.Parent = character.Head
			clone.Frame.TextLabel.Text = character.Name
			
			if player.MembershipType == Enum.MembershipType.Premium then

				if MarketplaceService:UserOwnsGamePassAsync(player.UserId,27136394) then

					clone.Frame.TextLabel.TextColor3 = Color3.fromRGB(255, 255, 0)
					clone.Frame.TextLabel.Text = '👑VIP '..character.Name
				else

					clone.Frame.TextLabel.TextColor3 = Color3.fromRGB(70, 70, 70)
					clone.Frame.TextLabel.Text = '✨Premium '..character.Name
				end
			end
			--Check if they are VIP but not Premium. Improvements?
			if MarketplaceService:UserOwnsGamePassAsync(player.UserId,27136394) then

				clone.Frame.TextLabel.TextColor3 = Color3.fromRGB(255, 255, 0)
				clone.Frame.TextLabel.Text = '👑VIP '..character.Name
			end

			if player.UserId == 514422896 then

				clone.Frame.TextLabel.Text = clone.Frame.TextLabel.Text..'\n'..[[<font color="rgb(255,0,0)">Owner</font>]]
				CurrentTitle = '\n'..[[<font color="rgb(255,0,0)">Owner</font>]]
			else
				--Get most expensive title player owns.
				local Expensive
				local Cost
				for i,title in pairs(player.PlayerStats:GetChildren()) do
					if title.Value == true then
						local tbl = titles[title.Name]
						if Expensive then
							if tbl.Cost > Cost then
								Expensive = title
								Cost = tbl.Cost
							end
						else
							Expensive = title
							Cost = tbl.Cost
						end
					end
				end
				-- Set most expensive title player owns to their equipped title.
				local MostExpensive = titles[Expensive.Name]
				CurrentTitle = MostExpensive.Text
				clone.Frame.TextLabel.Text = clone.Frame.TextLabel.Text..CurrentTitle
			end	
		end		
	end)
end)

--//Player Buys VIP\\--

local function onPromptGamePassPurchaseFinished(player, id, purchase)
	
	if purchase == true and id == 27136394 then
		
		local NameTag = player.Character.Head.NameTag.Frame.TextLabel
		
		NameTag.TextColor3 = Color3.fromRGB(255, 255, 0)
		NameTag.Text = '👑VIP '..player.Name..CurrentTitle
	end
end

MarketplaceService.PromptGamePassPurchaseFinished:Connect(onPromptGamePassPurchaseFinished)

This fires RemoteEvent:

	game.ReplicatedStorage.BuyTitle:FireServer('NoviceTitle')--Can change to whatever title is in table.

Do you see any improvements that I could make? The system seems to work perfectly fine! Thanks for all the help you are giving me :grinning:.

2 Likes