"String.split" method not working Error

I am trying to get the ID of a pants template and using it in PromptPurchase(). Im doing this by

string.split(Pants.PantsTemplate, "id=")[2]

Basically, it splits the URL in the pantsTemplate (which is rbxasset://CODE HERE), then gets the 2nd thing in the list (which is the id)
I am receiving an error that the first parameter is an instance, not a string. (therefore i cannot split, because an instance cannot be split)

Is PantsTemplate a property of “Pants”? or is it the name of some object value that’s stored inside the “Pants” object. I ran this code in my own studio and was able to do it successfully. If you could provide a screenshot of what the pants in the explorer tab (as well as its children, if any), that would be helpful.

It’s supposed to be the asset ID that i need.

The template of the pants is a decal, not the actual asset of the pants. So maybe that’s why it doesn’t work?

Shouldn’t it be a string? (shown in image)

From the url, there is no match that has “//” in it, only one “/”, which is why the function isn’t running.

Considering there will be pants templates that will either have
“rbxassetid://” … whatever here OR
“http://www.roblox.com/asset/?id=” … whatever here, I’d use a string.sub.

local templateId = tostring(workspace.Clothing.PantsTemplate) 
local id = nil
for index = 1, #templateId do
	if tonumber(string.sub(templateId, index, index)) ~= nil then
		id = tonumber(string.sub(templateId, index, #templateId))
		break   
	end
end

print(id)

IF ANYONE HAS A BETTER METHOD OF DOING THIS, PLEASE CHIP IN
I’m not an expert in string manipulation

EDIT: I screwed up my code one second
EDIT #2: Here’s the fixed version

2 Likes

I used the “pantsTemplate” on a “try pants” button and they worked perfectly.

1 Like

Thanks, I’ll try that and see if it works.

1 Like

Apparently I can’t use the function string.function() in this case, because it returns the same error:

21:08:16.333  Workspace.SellingPants.Buy.PurchaseButton.Script:3: invalid argument #1 to 'gsub' (string expected, got Instance) 

Hm, is this a piece of another code that was written?

Would you like to show the whole block of code to see if anyone (including myself) can help you out?

It seems like I accidently did PromptPurchase(string.split(player,PANTS, “//”))
my bad, but now it gives me an error saying “Unable to cast string to int64.”

PromptPurchase needs to things: The player instance and the ID

When you run the function, make sure that you define who the player is that you’re prompting, and make sure that the ID is in number form and not a string.

PromptPurchase(player, tonumber(string.split(PANTS, “//”)[2]))