Feedback on Asset given Tool Script

Edit: I’ve posted the working version of this script for anyone who made need to use something similar in the future!

I’m working on adding functionality to my game so that when users purchase a specific item in the group store they are awarded with various tools for having purchased it. The script I have now is functional, but I feel like it’s very inefficient, especially for dealing with multiple assets (four t-shirts) that are each awarding different gear (i.e. owning t-shirt #1 might get you tool #1, but owning t-shirt #2 might get you tool #1 and tool #2). If you can, please help show me a more efficient way of going about this/let me know what improvements and changes I can make.

Thank you!!

local Tool1 = "toolname"
local Asset1 = 123456789
local MarketplaceService = game:GetService("MarketplaceService")
local ServerStorage = game.GetService("ServerStorage")


game.Players.PlayerAdded:connect(function(Player)
    local StarterGear = Player:WaitForChild("StarterGear")
    if MarketplaceService:PlayerOwnsAsset(Asset1) then 
        if ServerStorage.Tools.FindFirstChild(Tool1) then
            ServerStorage.Tools.FindFirstChild(Tool1):Clone().Parent = StarterGear
        end
    end 
end)

It looks alright. Maybe convert into a module script if it will referenced multiple times (I have no idea if you will).

Because you have multiple assets, you want to make a single table containing both the tool names and marketplace assets. I have a recode here:

local Unlockables = { -- A list of the things that can be purchased with a gamepass,shirt,etc.
	--Template: {ToolName = "ToolName",Asset = AssetInt64}
	{ToolName = "toolname",Asset = 123456789};
	{ToolName = "anothertoolXD",Asset = 101010}
}
local MarketplaceService = game:GetService("MarketplaceService")
local ServerStorage = game.GetService("ServerStorage")


game.Players.PlayerAdded:connect(function(Player)
	local StarterGear = Player:WaitForChild("StarterGear")
	for i,v in pairs(Unlockables) do -- Check if the player owns any asset.
		if MarketplaceService:PlayerOwnsAsset(v.Asset) then 
			if ServerStorage.Tools.FindFirstChild(v.ToolName) then
				ServerStorage.Tools.FindFirstChild(v.ToolName):Clone().Parent = StarterGear
			end
		end
	end 
end)

With a table being checked, you wont have to make new variables all the time, making the game more optimized. Hope I was able to provide help :wink:

Edit: I have not tested this script. Give me a shout if it does not work.

It didn’t work, but I have a working fix (thank you though because the table format was really helpful!):

local Unlockables = { -- A list of the things that can be purchased with a given asset.
	--Template: {ToolName = "ToolName",Asset = AssetInt64}
	{ToolName = "Tool1",Asset = 123};
	{ToolName = "Tool2",Asset = 456}
}
local MarketplaceService = game:GetService("MarketplaceService")
local ServerStorage = game:GetService("ServerStorage")

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		print("Connected")
		local Backpack = Player:WaitForChild("Backpack")
		for i,v in pairs(Unlockables) do -- Check if the player owns any asset.
			if MarketplaceService:PlayerOwnsAsset(Player, v.Asset) then
				if ServerStorage.Tools[v.ToolName] then
					ServerStorage.Tools[v.ToolName]:Clone().Parent = Backpack
				end
			end
		end 
	end)
end)

I’ll pin this as the solution just in case anyone else needs to use this!

Is the asset a gamepass? If so you should use userownsgamepassasync

Welp, good to know you fixed it XD
Glad I can help!

The assets are t-shirts (old-fashioned I know haha) because the gamepass page was starting to get really cluttered.

1 Like