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

Why won't my simple hat equipping script work with multiple hats?

SERVER:

local insert = game:GetService("InsertService")

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(Player, ID)
	if tonumber(ID) == nil then return end
	local item = insert:LoadAsset(ID)
	local Child = item:GetChildren()[1]
	if Child:IsA("Decal") then
		Player.Character.Head.face.Texture = Child.Texture
	elseif Child:IsA("Accessory") then
		Player.Character.Humanoid:AddAccessory(Child)
	elseif Child:IsA("Shirt") then
		if Player.Character:FindFirstChildWhichIsA("Shirt") then
			Player.Character.Shirt.ShirtTemplate  = Child.ShirtTemplate
		else
			local Shirt = Instance.new("Shirt", Player.Character)
			Shirt.Name = "Shirt"
			Shirt.ShirtTemplate = Child.ShirtTemplate
		end
	elseif Child:IsA("Pants") then
		if Player.Character:FindFirstChildWhichIsA("Pants") then
			Player.Character.Pants.PantsTemplate = Child.PantsTemplate
		else
			local Pants = Instance.new("Pants", Player.Character)
			Pants.Name = "Pants"
			Pants.PantsTemplate = Child.PantsTemplate
		end
	end
end)

LOCAL:

script.Parent.TextButton.MouseButton1Click:Connect(function()
	for _, v in pairs(string.split(script.Parent.TextBox.Text, ",")) do
	print(v)
	game.ReplicatedStorage.RemoteEvent:FireServer(v)
	end
end)

ERROR:

  10:11:42.952 - Unable to cast string to int64
10:11:42.952 - Stack Begin
10:11:42.953 - Script 'Workspace.Script', Line 6
10:11:42.953 - Stack End

It works with one item, but not more than one. Any fixes?

1 answer

✓ Accepted answer

Replace your local with this script: This will make it so it will remove any spaces between your IDs and send it.

script.Parent.TextButton.MouseButton1Click:Connect(function()
	for _, v in pairs(string.split(script.Parent.TextBox.Text, ",")) do
	local item = string.match(v, "[%d]+")
	game.ReplicatedStorage.RemoteEvent:FireServer(item)
	end
end)

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